Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I have using the same query statement with no problem and a few day back suddenly I received an error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression"


appreciate if someone could help me to check if any problem with the query as below..

SQL
SELECT TOP (100) PERCENT YEAR(h.InvoiceDate)                                         AS Year,
                         MONTH(h.InvoiceDate)                                        AS Month,
                         h.CONumber,
                         CASE MONTH(InvoiceDate)
                           WHEN 1 THEN '1ST'
                           WHEN 2 THEN '1ST'
                           WHEN 3 THEN '1ST'
                           WHEN 4 THEN '2ND'
                           WHEN 5 THEN '2ND'
                           WHEN 6 THEN '2ND'
                           WHEN 7 THEN '3RD'
                           WHEN 8 THEN '3RD'
                           WHEN 9 THEN '3RD'
                           WHEN 10 THEN '4TH'
                           WHEN 11 THEN '4TH'
                           WHEN 12 THEN '4TH'
                         END                                                         AS Quarter,
                         i.ItemNumber,
                         i.ItemDescription,
                         CASE PRODUCT_GROUP
                           WHEN 00 THEN 'LAB'
                           WHEN 02 THEN 'PRO'
                           WHEN 10 THEN 'ANA'
                         END                                                         AS Division,
                         i.FamilySubgroup,
                         i.FamilyItemNumber,
                         c.CustomerName,
                         h.ShipToDeliveryLocationName,
                         CASE
                           WHEN CustomerCurrencyCode = '00000' THEN 'MYR'
                           ELSE CustomerCurrencyCode
                         END                                                         AS Currency,
                         ISNULL ((SELECT xl.InvoiceForeignUnitPrice
                                  FROM   dbo.FS_ARInvoiceHeader AS xh
                                         LEFT OUTER JOIN dbo.FS_ARInvoiceLine AS xl
                                                      ON xh.ARInvoiceHeaderKey = xl.ARInvoiceHeaderKey
                                  WHERE  ( YEAR(xh.InvoiceDate) >= '2012' )
                                         AND ( xh.InvoiceStatus <> 'X' )
                                         AND ( xh.InvoiceStatus <> 'U' )
                                         AND ( xl.LineItemNumber NOT LIKE 'PACK%' )
                                         AND ( xl.LineItemNumber NOT LIKE 'FREIG%' )
                                         AND ( xh.CustomerID NOT LIKE 'ZZDEMO%' )
                                         AND ( i.ItemNumber IS NOT NULL )
                                         AND ( xh.InvoiceType = 'R' )
                                         AND ( xh.CONumber = h.CONumber )
                                         AND ( xl.LineItemNumber = l.LineItemNumber )
                                         AND ( xh.ShipmentNumberString = h.ShipmentNumberString )
                                         AND ( xh.InvoiceDate > h.InvoiceDate )
                                  GROUP  BY xl.InvoiceForeignUnitPrice), 0)          AS CreditUnitPrice,
                         ISNULL ((SELECT AVG(xl.ShipQuantity) AS Expr1
                                  FROM   dbo.FS_ARInvoiceHeader AS xh
                                         LEFT OUTER JOIN dbo.FS_ARInvoiceLine AS xl
                                                      ON xh.ARInvoiceHeaderKey = xl.ARInvoiceHeaderKey
                                  WHERE  ( YEAR(xh.InvoiceDate) >= '2012' )
                                         AND ( h.InvoiceStatus <> 'X' )
                                         AND ( h.InvoiceStatus <> 'U' )
                                         AND ( l.LineItemNumber NOT LIKE 'PACK%' )
                                         AND ( l.LineItemNumber NOT LIKE 'FREIG%' )
                                         AND ( c.CustomerID NOT LIKE 'ZZDEMO%' )
                                         AND ( i.ItemNumber IS NOT NULL )
                                         AND ( xh.InvoiceType = 'R' )
                                         AND ( xh.CONumber = h.CONumber )
                                         AND ( xl.LineItemNumber = l.LineItemNumber )
                                         AND ( xh.ShipmentNumberString = h.ShipmentNumberString )
                                         AND ( xh.InvoiceDate > h.InvoiceDate )), 0) AS ReturnQty,
                         CASE
                           WHEN InvoiceType = 'I' THEN InvoiceForeignUnitPrice
                           ELSE '0'
                         END                                                         AS InvoiceUnitPrice,
                         l.ShipQuantity,
                         i.MakeBuyCode,
                         CASE MakeBuyCode
                           WHEN 'B' THEN 'N'
                           WHEN 'S' THEN 'N'
                           WHEN 'M' THEN ( CASE CustomerClass4
                                             WHEN '' THEN 'Y'
                                             WHEN '2' THEN 'N'
                                             WHEN '1' THEN ( CASE FamilySubgroup
                                                               WHEN '0205' THEN 'N'
                                                               WHEN '0016' THEN 'N'
                                                               WHEN '0017' THEN 'N'
                                                               WHEN '1021' THEN 'N'
                                                               WHEN '1022' THEN 'N'
                                                               ELSE 'Y'
                                                             END )
                                           END )
                         END                                                         AS Lic
FROM   dbo.FS_ARInvoiceHeader AS h
       LEFT OUTER JOIN dbo.FS_ARInvoiceLine AS l
                    ON h.ARInvoiceHeaderKey = l.ARInvoiceHeaderKey
       LEFT OUTER JOIN dbo.FS_Item AS i
                    ON l.ItemKey = i.ItemKey
       LEFT OUTER JOIN dbo.FS_Customer AS c
                    ON h.CustomerID = c.CustomerID
       LEFT OUTER JOIN dbo.zPRODUCT_CODE AS p
                    ON i.FamilySubgroup = p.PRODUCT_CODE
WHERE  ( YEAR(h.InvoiceDate) >= '2012' )
       AND ( h.InvoiceStatus <> 'X' )
       AND ( h.InvoiceStatus <> 'U' )
       AND ( l.LineItemNumber NOT LIKE 'PACK%' )
       AND ( l.LineItemNumber NOT LIKE 'FREIG%' )
       AND ( c.CustomerID NOT LIKE 'ZZDEMO%' )
       AND ( i.ItemNumber IS NOT NULL )
       AND ( h.InvoiceType = 'I' )
ORDER  BY Month,
          h.CONumber
Posted
Updated 3-Apr-14 6:22am
v2

It is quite often asked question. Please, see[^].

In my opinion, your query is too long. Try to write it in simple manner. Check each subquery. You can add TOP(1) instruction together with SELECT statement for subquery. But i need to warn you: it's not perfect solution...
 
Share this answer
 
Andrius Leonavicius writes that you have to check the check the subselect starting with:
ISNULL ((SELECT xl.InvoiceForeignUnitPrice
Well, it's actually that specific line that is the problem. You need to aggregate it using Min(), Max(), Avg() or whatever is making best sense for your business logics, and at the same time remove the GROUP BY xl.InvoiceForeignUnitPrice.
 
Share this answer
 
Comments
Maciej Los 3-Apr-14 15:45pm    
Good alternative ;)
+5!
Andrius Leonavicius 3-Apr-14 17:15pm    
+5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900