使用EXTRACT时选择特定年份

时间:2016-08-31 21:06:01

标签: sql oracle

我试图提取一些商品数据,但我却陷入了如何将其缩小到特定年份。

我有以下代码......

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年时,这将返回所有可用年份。

非常感谢任何和所有帮助!

1 个答案:

答案 0 :(得分: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