任何人都可以帮助我请计算2行或更多行记录见下面的查询。
SELECT docs_DocCode, SUM(invd_Qty) AS Total_IN
FROM dbo.SI_InventoryDetail
WHERE (docs_DocCode = 'D.C') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'STO') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'SAO') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'IOB') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'GRN') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'STI') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'SAI') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'SRN') AND (itm_ItemCode = '0101050000083') OR
(docs_DocCode = 'PRN') AND (itm_ItemCode = '0101050000083')
GROUP BY docs_DocCode
ABove查询给我结果如下
dics_doccode Total
D.C 328.00000000
GRN 205.00000000
IOB 96.00000000
SAI 66.00000000
SAO 51.00000000
SRN 14.00000000
STI 150.00000000
STO 150.00000000
现在我想加入(D.C + STI + SAI) - (GRN + STI + SAI + IOB)=一个值
我正在使用SQL SERVER 2000
Thankx
答案 0 :(得分:1)
SELECT SUM(CASE
WHEN docs_DocCode IN ('D.C', 'STI', 'SAI') THEN invd_Qty
WHEN docs_DocCode IN ('GRN', 'STI', 'SAI', 'IOB') THEN -1 * invd_Qty
ELSE 0
END) AS One_Value
....
请注意,不会添加invd_Qty
中不属于任何'D.C', 'STI', 'SAI', 'GRN', 'STI', 'SAI', 'IOB'
的{{1}}。
PS:(D.C + STI + SAI ) - ( GRN + STI + SAI + IOB) - 我强调了无意义的(??)部分。它等于D.C. - (SAI + IOB)
答案 1 :(得分:0)
我解决了我的问题感谢“Zerkms”以下是我的查询及其结果
选择Itm_itemcode,
SUM(CASE WHEN docs_DocCode IN ('D.C', 'STO', 'SAO','PRN') THEN INVD_QTY END ) -
SUM(CASE WHEN docs_DocCode IN ('GRN', 'STI', 'SAI','SRN','IOB') THEN invd_Qty END) AS Qty
来自SI_InventoryDetail
其中Itm_itemcode ='0101050000083'
按Itm_itemcode分组
<强>结果强>
Item_code数量
0101050000083 -2.00000000
Thankx 勇敢的阿里khatri