SQL Msg 512

Hello,
Can you please help,
I'm getting the error message
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value

SELECT  FO.OrderNo AS [Order Number],  CONVERT(varchar(20),FO.OrderDate,101) AS [Date],
CL.AccountNumber,  CL.AccountCode, FO.TotalAmount AS [TotalAmt],  FOI.InvoiceNumber AS [InvoiceNumber],
(Select TransactionDate from AROpenTransactions where AROpenTransactions.TransactionNumber = FOI.InvoiceNumber) AS [InvoiceDate]
FROM FinalizedOrders FO
INNER JOIN Clients CL ON FO.AccountNumber = CL.AccountNumber
LEFT JOIN ServiceTypes ST ON FO.ServiceTypeID = ST.ServiceTypeID
LEFT JOIN VehicleTypes VT ON FO.VehicleTypeID = VT.VehicleTypeID
INNER JOIN FinalizedOrdersInvoices FOI ON FOI.OrderNo = FO.OrderNo
Where ((FO.AccountNumber in (48,391,1181)))
AND (CL.LastInvoiceDate >= '03-01-2015' and CL.LastInvoiceDate <= '03-15-2015')
and FOI.InvoiceNumber > 1

I tried to use
INNER JOIN AROpenTransactions AR ON CL.LastInvoiceDate = AR.TransactionDate
-----
AND (AR.TransactionDate >= '03-01-2015' and AR.TransactionDate <= '03-15-2015')

I still get the same error.

Thank you,
W.E.BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, please post formatted code. Makes reading easier:

SELECT  FO.OrderNo AS [Order Number] ,
        CONVERT(VARCHAR(20), FO.OrderDate, 101) AS [Date] ,
        CL.AccountNumber ,
        CL.AccountCode ,
        FO.TotalAmount AS [TotalAmt] ,
        FOI.InvoiceNumber AS [InvoiceNumber] ,
        ( SELECT    TransactionDate
          FROM      AROpenTransactions
          WHERE     AROpenTransactions.TransactionNumber = FOI.InvoiceNumber
        ) AS [InvoiceDate]
FROM    FinalizedOrders FO
        INNER JOIN Clients CL ON FO.AccountNumber = CL.AccountNumber
        LEFT JOIN ServiceTypes ST ON FO.ServiceTypeID = ST.ServiceTypeID
        LEFT JOIN VehicleTypes VT ON FO.VehicleTypeID = VT.VehicleTypeID
        INNER JOIN FinalizedOrdersInvoices FOI ON FOI.OrderNo = FO.OrderNo
WHERE   FO.AccountNumber IN ( 48, 391, 1181 )
        AND CL.LastInvoiceDate >= '03-01-2015'
        AND CL.LastInvoiceDate <= '03-15-2015'
        AND FOI.InvoiceNumber > 1

Open in new window


The error message means that this sub-query:

 ( SELECT    TransactionDate
          FROM      AROpenTransactions
          WHERE     AROpenTransactions.TransactionNumber = FOI.InvoiceNumber
        ) AS [InvoiceDate]

Open in new window


returns more than one row. This is not allowd, cause it must return a single row and single column to make a scalar from it for the outer column value.

Run this to test your data:

SELECT  TransactionNumber ,
        COUNT(*)
FROM    AROpenTransactions
GROUP BY TransactionNumber
HAVING  COUNT(*) > 1;
       

Open in new window


There must be at least one row..
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Looks like the
Select TransactionDate from AROpenTransactions where AROpenTransactions.TransactionNumber = FOI.InvoiceNumber
is returning more that one date. You need to assure that query only returns one row. What's the PK for AROpenTransactions table?
0
Scott PletcherSenior DBACommented:
You can adjust as needed but this should give you the last matching TransactionDate.  Adjust the AR.TransactionDate conditions if/as needed.  Btw, I switched to format 'YYYYMMDD' as that is universal, whereas yyyy-mm-dd can be interpreted differently on some SQL systems.


SELECT  FO.OrderNo AS [Order Number],  CONVERT(varchar(20),FO.OrderDate,101) AS [Date],
 CL.AccountNumber,  CL.AccountCode, FO.TotalAmount AS [TotalAmt],  FOI.InvoiceNumber AS [InvoiceNumber],
 (Select Top (1) TransactionDate from AROpenTransactions AR where AR.TransactionNumber = FOI.InvoiceNumber and AR.TransactionDate >= '20150301' and AR.TransactionDate < '20150316' order by AR.TransactionDate DESC) AS [InvoiceDate]
 FROM FinalizedOrders FO
 INNER JOIN Clients CL ON FO.AccountNumber = CL.AccountNumber
 LEFT JOIN ServiceTypes ST ON FO.ServiceTypeID = ST.ServiceTypeID
 LEFT JOIN VehicleTypes VT ON FO.VehicleTypeID = VT.VehicleTypeID
 INNER JOIN FinalizedOrdersInvoices FOI ON FOI.OrderNo = FO.OrderNo
 Where ((FO.AccountNumber in (48,391,1181)))
 AND (CL.LastInvoiceDate >= '03-01-2015' and CL.LastInvoiceDate <= '03-15-2015')
 and FOI.InvoiceNumber > 1
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

W.E.BAuthor Commented:
Hello,
Scott
It's returning all rows for the 3 accounts . (Including the AR.TransactionDate   NULL).
I tired to add    and AR.TransactionDate IS NOT null,
didn't work,
if I can exclude the AR.TransactionDate that are Null,

ste5an
SELECT  TransactionNumber ,    COUNT(*) FROM    AROpenTransactions
GROUP BY TransactionNumber HAVING  COUNT(*) > 1;

it did return thousands of rows.

Thank you,
0
Scott PletcherSenior DBACommented:
A subquery within the SELECT is never going to increase or reduce the number of rows returned by the main query, it simply provides a column value.  I was addressing the problem you asked about, i.e., preventing that subquery from returning multiple values.

If you just want to ignore any results that don't have a corresponding TransactionDate, then we need to use a CROSS APPLY rather than a subquery:

SELECT  FO.OrderNo AS [Order Number],  CONVERT(varchar(20),FO.OrderDate,101) AS [Date],
 CL.AccountNumber,  CL.AccountCode, FO.TotalAmount AS [TotalAmt],  FOI.InvoiceNumber AS [InvoiceNumber],
 AR_ca.InvoiceDate
 FROM FinalizedOrders FO
 INNER JOIN Clients CL ON FO.AccountNumber = CL.AccountNumber
 LEFT JOIN ServiceTypes ST ON FO.ServiceTypeID = ST.ServiceTypeID
 LEFT JOIN VehicleTypes VT ON FO.VehicleTypeID = VT.VehicleTypeID
 INNER JOIN FinalizedOrdersInvoices FOI ON FOI.OrderNo = FO.OrderNo
 CROSS APPLY (
     SELECT TOP (1) TransactionDate AS [InvoiceDate]
     FROM AROpenTransactions AR
     WHERE AR.TransactionNumber = FOI.InvoiceNumber and
           AR.TransactionDate >= '20150301' and
           AR.TransactionDate <  '20150316'
     ORDER BY AR.TransactionDate DESC
 ) AS AR_ca

 Where ((FO.AccountNumber in (48,391,1181)))
 AND (CL.LastInvoiceDate >= '03-01-2015' and CL.LastInvoiceDate <= '03-15-2015')
 and FOI.InvoiceNumber > 1
 and AR_ca.InvoiceDate IS NOT NULL--technically not needed because CROSS APPLY only returns matches, but just in case
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
W.E.BAuthor Commented:
Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.