2011-11-11 217 views
2

我需要完成以下操作,但需要對EntryValue進行SUM處理,其中EntryType ='C' - EntryType ='D'但不確定將其包含在哪裏。SQL Select Sum查詢

SELECT SUM(EntryValue) AS EntryValue, Left(Warehouse,1) AS Branch 
FROM GenJournalDetail INNER JOIN InvJournalCtl ON (GenJournalDetail.GlPeriod = InvJournalCtl.LedgPeriod) AND (GenJournalDetail.GlYear = InvJournalCtl.YearPostLedg) AND (GenJournalDetail.Journal = InvJournalCtl.GlJournal) 
WHERE GenJournalDetail.GlCode = 'Z5207550' AND GlYear = '2011' AND GlPeriod = '10' 
GROUP BY Left(InvJournalCtl.Warehouse,1) 

回答

2

選擇它然後對它進行分組;

SELECT SUM(EntryValue) AS EntryValueTotal, EntryType, Left(Warehouse,1) AS Branch 
... 
GROUP BY Left(InvJournalCtl.Warehouse,1), EntryType 

編輯我不知道你想扣除;

SELECT 
    SUM(CASE EntryType 
     WHEN 'C' THEN EntryValue 
     ELSE 0 END 
    ) 
    - 
    SUM(CASE EntryType 
     WHEN 'D' THEN EntryValue 
     ELSE 0 END 
    ) AS EntryValueTotal, 
    Left(Warehouse,1) AS Branch 
FROM GenJournalDetail 
    INNER JOIN InvJournalCtl ON (GenJournalDetail.GlPeriod = InvJournalCtl.LedgPeriod) AND (GenJournalDetail.GlYear = InvJournalCtl.YearPostLedg) AND (GenJournalDetail.Journal = InvJournalCtl.GlJournal) 
WHERE 
    GenJournalDetail.GlCode = 'Z5207550' AND GlYear = '2011' AND GlPeriod = '10' 
GROUP BY Left(InvJournalCtl.Warehouse,1) 
+0

我很抱歉,但這個答案沒有任何意義。 –

+0

對不起,我有大腦的EntryValue,我需要改進的領域是如果EntryType ='D'沒有輸入值 – lewiscooksey

+0

@ t-clausen.dk是的你的權利,我沒有給予足夠的關注的問題,SUM EntryValue)EntryValue可能是一個字符沒有意義 –

0

如果您需要總結列添加它的選擇和組:

SELECT SUM(EntryValue) AS TotEntry, ... 
... 
GROUP BY ... 

你不需要團的集合列;你需要過濾查詢嗎?

+0

我是否需要按EntryType進行分組?我需要從另一個減去一個總數。 – lewiscooksey

+0

對不起,我不明白你需要什麼。你想要查詢什麼? –

+0

不要嘗試總結,而你選擇入境價值,這是錯誤的! –