在我目前工作的一个项目中,我们需要获得每月、每季度和每年的客户销售额。我尝试了一个如下所示的查询,该查询今天得到了正确的结果。但我也需要去年的销售结果。(去年月销售额是那个月的总和)
我真的需要在这里快速修复,提前感谢每条评论
SELECT
CUSTOMER,
PRODUCT,
EXTRACT(MONTH FROM DAY) MONTH,
EXTRACT(YEAR FROM DAY) YEAR,
SUM(
CASE
WHEN DAY >= TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS MONTH_SALE,
SUM(
CASE
WHEN DAY >= add_months(TRUNC(sysdate,'MM'),-3)
AND DAY < TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS THREE_MONTHL_SALE,
SUM(
CASE
WHEN DAY >= add_months(TRUNC(sysdate,'MM'),-6)
AND DAY < TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS SIX_MONTHL_SALE,
SUM(
CASE
WHEN DAY >= add_months(TRUNC(sysdate,'MM'),-12)
AND DAY < TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS YEAR_SALE
FROM
SALES_TABLE
GROUP BY
CUSTOMER,
PRODUCT,
EXTRACT(MONTH FROM DAY),
EXTRACT(YEAR FROM DAY)