2015-12-20 184 views
0

我有以下查詢。請準備好,首先SQL查詢返回子查詢返回的值超過1個錯誤

select 
    X.ITEMCODE, X.[ITEM_DESCRIPTION], X.[OPENING_BALANCE], 
    X.[SALES], X.[TOTAL_REJECTS], x.[REJECTION_DISEASES] 
    (X.[SALES] - X.[TOTAL_REJECTS]) as [PRODUCTION_TOTAL], 
    X. [DOCDATE], 
    (X.[OPENING_BALANCE] + (X.[SALES]-X.[TOTAL_REJECTS])) AS [CLOSING_BALANCE] 
from 
    (SELECT DISTINCT 
     T0.ItemCode AS ITEMCODE, T0.ItemName AS [ITEM_DESCRIPTION], 
     ISNULL((SELECT SUM(T1.OnHand) 
       FROM OITW T1 
       WHERE T1.WhsCode = 'FGS' 
        AND T1.ItemCode = T0.ItemCode), 0) AS [OPENING_BALANCE], 
     ISNULL((SELECT SUM(T1.[Quantity]) 
       FROM INV1 T1 
       WHERE T0.[ItemCode] = T1.[ItemCode]), 0) AS [SALES] , 
     (SELECT T1.[DocDate] FROM INV1 T1 
      WHERE T0.[ItemCode] = T1.[ItemCode]) AS [DOCDATE], 
     ISNULL((SELECT SUM(cast(T1.[Quantity] as int)) 
       FROM IGE1 T1 
       WHERE T0.[ItemCode] = T1.[ItemCode]), 0) AS [TOTAL_REJECTS], 
     ISNULL((SELECT T1.[U_RejectionOthers] 
       FROM IGE1 T1 
       WHERE T0.[ItemCode] = T1.[ItemCode]), 0) AS [REJECTION_DISEASES] 
    from 
     OITM T0 
    where 
     T0.[ItmsGrpCod] in ('104', '121', '122', '123', '124', '125', '126', '127', '129', '130', '134', '135', '136', '139', '140', '141', '142', '143', '144', '145')) X 
WHERE 
    X.[OPENING_BALANCE] > 0 OR X.[TOTAL_REJECTS] > 0 OR X.[SALES] > 0 

看看在這段代碼中,當我試圖在主SELECT語句添加X.[DOCDATE]x.[REJECTION_DISEASES]然後我收到以下錯誤

子查詢返回多個值。當 子查詢遵循=,!=,<,< =,>,> =或當子查詢用作 表達式時,這是不允許的。

請高手幫我解決。

回答

2

我懷疑下面sub-query回報超過一排的多個給定條件where條款

Isnull((SELECT T1.[u_rejectionothers] 
     FROM ige1 T1 
     WHERE T0.[itemcode] = T1.[itemcode]), 0) AS[REJECTION_DISEASES] 

要麼你需要修復的where子句返回一行或TOPOrder by返回一行

修復where子句。放入where子句,通過它可以識別ige1

Isnull((SELECT T1.[u_rejectionothers] 
     FROM ige1 T1 
     WHERE T0.[itemcode] = T1.[itemcode] 
      AND T1.somecol = 'XXX'), 0) AS[ REJECTION_DISEASES] 

sub-query

Isnull((SELECT TOP 1 T1.[u_rejectionothers] 
     FROM ige1 T1 
     WHERE T0.[itemcode] = T1.[itemcode] 
     Order by somecol), 0) AS [REJECTION_DISEASES] 
+0

添加TOPOrder by獨特的線上的所有條件,爲什麼你在這裏補充頂部1? @ VR46 – Kirat

+0

'Top'避免子查詢返回多個行錯誤。我還提到了另一種更有意義的方法 –

相關問題