2013-09-30 118 views
0

我有一個運行良好麻煩與Select語句

SELECT 
    division, 
    division_name, 
    object_account, 
    account_description, 
    Sum(value) AS PreferedSpend 
FROM jde_dw.blanket_rpt 
WHERE division = 02 
AND vendor_type = 'PV' 
AND month = 07 
AND year = 2013 
GROUP BY division, division_name, object_account, account_description 
ORDER BY division 

現在我想增加一列,顯示總花費將是相同的查詢如上,但沒有在其中的vendor_type條件下選擇查詢聲明。

所以最終的查詢將顯示事業部,division_name,Object_account,ACCOUNT_DESCRIPTION,值(首選)和價值(總包括一切)

我怎樣才能做到這一點?

+3

我不知道你問這裏 –

+0

我認爲你正在尋找[級聯(HTTP:/ /technet.microsoft.com/en-us/library/ms177561.aspx) – 2013-09-30 15:07:47

+1

這需要更多描述:「包含所有內容的總數」。也許你可以向我們展示一些示例數據和結果? –

回答

1

移動狀態裏面SUM()這樣只有匹配的行貢獻的總和:

SELECT 
    division, 
    division_name, 
    object_account, 
    account_description, 
    SUM(CASE WHEN vendor_type = 'PV' THEN value ELSE 0 END) as PreferedSpend, 
    SUM(value) AS TotalSpend 
FROM jde_dw.blanket_rpt 
WHERE division = 02 
AND month = 07 
AND year = 2013 
GROUP BY division, division_name, object_account, account_description 
ORDER BY division