2012-12-26 374 views
0

在我工作的項目中,我們需要獲得每月,每季度和每年的客戶銷售總額。我試了下面顯示的查詢,今天得到正確的結果。但我還需要去年的銷售業績。 (過去幾年月度銷售是個總和)oracle銷售總查詢

我真的需要一個快速修復這裏,感謝每一個評論提前

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) 
+0

問題是什麼用您的查詢?你能提供你正在尋找的結果嗎? –

+0

您的查詢將根據當前日期爲您提供三個月的分組......如果您在四分之一個月結束的月份運行查詢,那麼這隻會與公司宿舍等同。所以,我的問題是,當你說「宿舍」時,你的意思是1月 - 2月 - 3月總是第1季度,還是你需要它來改變你的查詢的方式? – Chipmonkey

回答

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