我尝试通过运行以下SQL代码在表底部运行总行。
SELECT
T1.[ItemCode],T4.[ItmsGrpNam],
CASE
WHEN T3.[OnHand] - SUM(T1.[OpenQty]) < SUM(T1.[OpenQty])
THEN T3.[OnHand] - SUM(T1.[OpenQty])
ELSE
SUM(T1.[OpenQty])
END As 'Missing Quantity'
FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN OITB T4 ON T2.ItmsGrpCod=T4.ItmsGrpCod
WHERE
T3.[WhsCode] = T1.[WhsCode]
and T0.[DocDueDate] <= '[%D1]'
and (T4.[ItmsGrpNam] = 'Commercial' Or T4.[ItmsGrpNam] = 'Residentiel')
GROUP BY T1.[ItemCode],T3.[OnHand],T4.[ItmsGrpNam]
HAVING T3.[OnHand] - SUM(T1.[OpenQty]) < 0 Or SUM(T1.[OpenQty]) < 0
UNION ALL
SELECT 'See', 'Total Missing Quantity' , CASE
WHEN T3.[OnHand] - SUM(T1.[OpenQty]) < SUM(T1.[OpenQty])
THEN T3.[OnHand] - SUM(T1.[OpenQty])
ELSE
SUM(T1.[OpenQty])
END
FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN OITB T4 ON T2.ItmsGrpCod=T4.ItmsGrpCod
WHERE
T3.[WhsCode] = T1.[WhsCode]
and T0.[DocDueDate] <= '[%D1]'
and (T4.[ItmsGrpNam] = 'Commercial' Or T4.[ItmsGrpNam] = 'Residentiel')
我有此错误消息' 1)。 [Microsoft] [SQL Server Native Client 11.0] [SQL Server]“OITW.OnHand”列在选择列表中无效,因为它不包含在聚合函数或BY GROUP子句中。 2)。 [Microsoft] [SQL Server Native Client 11.0] [SQL Server]无法准备语句。 '用户定义值'(CSHS)'
当我尝试
时GROUP BY T1.[ItemCode],T3.[OnHand],T4.[ItmsGrpNam]
HAVING T3.[OnHand] - SUM(T1.[OpenQty]) < 0 Or SUM(T1.[OpenQty]) < 0
在查询的第二部分中,当我只想要一个总和的行时,我得到多行。
我花了一天时间来解决这个问题,NET上有多个答案,但我不知道我做错了什么。
是否有人可以帮我解决这个问题?提前谢谢。