0

在我目前工作的一个项目中,我们需要获得每月、每季度和每年的客户销售额。我尝试了一个如下所示的查询,该查询今天得到了正确的结果。但我也需要去年的销售结果。(去年月销售额是那个月的总和)

我真的需要在这里快速修复,提前感谢每条评论

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)
4

2 回答 2

0

这可能有助于满足您的需求……它为您提供每个月和每个季度以及全年、每年的总计。如果你想要一个特定的年份,我建议添加“where EXTRACT(YEAR FROM DAY) = 2012”或类似的子句。

我相信这是您要求的,但这与您提供的样本不同。您提供的样本提供了过去 3、6、9 和 12 个月的销售额,这与季度年和月不同。不过,如果需要,您可以轻松混合和匹配。

SELECT    
CUSTOMER,        
PRODUCT,
EXTRACT(YEAR FROM DAY) YEAR,
SUM(DAILY_SALE) as YEAR_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '01' THEN DAILY_SALE ELSE 0 END) AS MONTH_01_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '02' THEN DAILY_SALE ELSE 0 END) AS MONTH_02_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '03' THEN DAILY_SALE ELSE 0 END) AS MONTH_03_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '04' THEN DAILY_SALE ELSE 0 END) AS MONTH_04_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '05' THEN DAILY_SALE ELSE 0 END) AS MONTH_05_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '06' THEN DAILY_SALE ELSE 0 END) AS MONTH_06_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '07' THEN DAILY_SALE ELSE 0 END) AS MONTH_07_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '08' THEN DAILY_SALE ELSE 0 END) AS MONTH_08_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '09' THEN DAILY_SALE ELSE 0 END) AS MONTH_09_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '10' THEN DAILY_SALE ELSE 0 END) AS MONTH_10_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '11' THEN DAILY_SALE ELSE 0 END) AS MONTH_11_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '12' THEN DAILY_SALE ELSE 0 END) AS MONTH_12_SALES,
SUM(
    CASE
        WHEN TO_CHAR(DAY, 'MM') IN ('10', '11', '12')
        THEN DAILY_SALE
        ELSE 0
    END) AS Q4_SALES,
SUM(
    CASE
        WHEN TO_CHAR(DAY, 'MM') IN ('07', '08', '09')
        THEN DAILY_SALE
        ELSE 0
    END) AS Q3_SALES,
SUM(
    CASE
        WHEN TO_CHAR(DAY, 'MM') IN ('04', '05', '06')
        THEN DAILY_SALE
        ELSE 0
    END) AS Q2_SALES,
SUM(
    CASE
        WHEN TO_CHAR(DAY, 'MM') IN ('01', '02', '03')
        THEN DAILY_SALE
        ELSE 0
    END) AS Q1_SALES,
FROM
SALES_TABLE
GROUP BY
CUSTOMER,
PRODUCT,
EXTRACT(YEAR FROM DAY)
于 2012-12-26T16:29:10.020 回答
0

这可能不是您所需要的,但它可能会有所帮助:

select a.Product_Name, 
d.Company_Name, 
to_char(Order_Date, 'YYYY') as OrderYear,
sum(case to_char(c.Order_Date, 'Q') when '1' 
    then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 1",
sum(case to_char(c.Order_Date, 'Q') when '2' 
    then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 2",
sum(case to_char(c.Order_Date, 'Q') when '3' 
    then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 3",
sum(case to_char(c.Order_Date, 'Q') when '4' 
    then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 4" 
from Products a 
....

此外,请在此处查找您可能需要的良好示例:http: //docs.oracle.com/html/B13915_04/appendix.htm#BEHJBJDE

于 2012-12-26T19:53:53.727 回答