2016-08-31 90 views
0

我想拉幾個SUM,但我陷入瞭如何縮小到特定的一年。使用EXTRACT選擇特定年份

我有以下代碼...

SELECT SITE_ID, Extract(YEAR FROM DATE_ORDERED) YEAR, Extract(MONTH FROM DATE_ORDERED) MONTH, SUM(TOTAL_PRICE), SUM(TOTAL_PRICE), SUM(TOTAL_SAVINGS) FROM DB.ACTUAL_SAVINGS_MVIEW WHERE SITE_ID = 561 GROUP BY SITE_ID, Extract(YEAR FROM DATE_ORDERED), Extract(MONTH FROM DATE_ORDERED) ORDER BY YEAR DESC, MONTH DESC

這將返回所有可用的幾年,當我只想找2016年

任何和所有幫助將不勝感激!

+0

你沒有被今年限制在where子句 –

+0

我難以置信哈哈哈...很抱歉..它是一個漫長的一天... –

+0

'Extract(YEAR FROM DATE_ORDERED)'可能不會使用任何索引/分區,更好地使用可搜索條件:'DATE_ORDERED BETWEEN DATE'2016-01-01'和DATE'2016-12-31'' – dnoeth

回答

3

如何將它添加到您的where子句:

SELECT SITE_ID, 
    Extract(YEAR FROM DATE_ORDERED) YEAR, 
    Extract(MONTH FROM DATE_ORDERED) MONTH, 
    SUM(TOTAL_PRICE), 
    SUM(TOTAL_PRICE), 
    SUM(TOTAL_SAVINGS) 
FROM DB.ACTUAL_SAVINGS_MVIEW 
WHERE SITE_ID = 561 
AND Extract(YEAR FROM DATE_ORDERED) = 2016 
GROUP BY SITE_ID, 
    Extract(YEAR FROM DATE_ORDERED), 
    Extract(MONTH FROM DATE_ORDERED) 
ORDER BY YEAR DESC, 
    MONTH DESC 
+0

Yea ... I我不能相信我錯過了這一天......漫長的一天一定讓我失去了我的想法......我現在感到很蠢,哈哈哈......感謝這一點 –