2010-12-14 64 views
0

任何一個幫我請請計算兩行或更多行記錄見下面的查詢。計算黑白兩行

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 

上述查詢給我的結果如下

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 

現在,我想補充(特區+ STI + SAI) - (GRN + STI + SAI + IOB)=一個值

,我使用SQL SERVER 2000

Thankx

回答

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'將不會被添加。

PS:(特區+ STI + SAI) - (GRN + STI + SAI + IOB) - 我已經強調了毫無意義的(?)部分。它等於D.C. - (SAI + IOB)

+0

親愛THankx的快速回復,但你給出的查詢工作不 – 2010-12-14 06:07:37

+0

我我要在哪裏添加Cluse在這裏確定我的ITEM基本計算是針對當前股票狀態.... – 2010-12-14 06:11:34

+0

@Brave ali Khatri:'不工作'是不夠的詳細描述一個問題。我不能得到你的完整的第二評論:-S – zerkms 2010-12-14 06:14:43

0

我解決我的問題thankx到「Zerkms」下面是我的查詢,並將其結果

SELECT 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_InventoryDe​​tail

其中Itm_itemcode = '0101050000083'

group by Itm_itemcode

結果

ITEM_CODE數量
0101050000083 -2.00000000

Thankx 勇敢阿里·卡特里

+0

如果您發現我的答案有幫助,您可以在表決數小於3的地方使用綠色標記標記下者。 – zerkms 2010-12-14 07:24:34