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]
然後我收到以下錯誤
子查詢返回多個值。當 子查詢遵循=,!=,<,< =,>,> =或當子查詢用作 表達式時,這是不允許的。
請高手幫我解決。
添加
TOP
加Order by
獨特的線上的所有條件,爲什麼你在這裏補充頂部1? @ VR46 – Kirat'Top'避免子查詢返回多個行錯誤。我還提到了另一種更有意義的方法 –