2014-10-09 110 views
0

需要在這裏快速幫助 我有這個小項目,我有這個報告,有命令的總數與命令的日期(這是從我的另一個問題的延續實際上) 所以這裏就是我一直在做使用提取和last_day函數獲取每天的平均值

SELECT sf.ORDER_QNT, dd.ACTUAL_DATE, dd.MONTH_NUMBER 
FROM sales_fact sf, 
     date_dim dd 
WHERE dd.date_id = sf.date_id 
AND dd.MONTH_NUMBER = 1; 

ORDER_QNT   ACTUAL_DATE MONTH_NUMBER 
------------------------------------------- 
     1100    05/01/13   1 
     100    05/01/13   1 
     140    06/01/13   1 
     110    07/01/13   1 
     200    08/01/13   1 
     500    08/01/13   1 
     230    08/01/13   1 
     500    08/01/13   1 
     200    08/01/13   1 
     53    15/01/13   1 
     53    22/01/13   1 

現在,我想通過LAST_DAY函數來獲得平均爲每月(平均每天)。

SELECT sum(sf.ORDER_QNT)/EXTRACT(DAY FROM LAST_DAY(to_date('05/01/13','dd/mm/rr'))) as AVGPERDAY, 
     dd.month_number 
FROM sales_fact sf, 
     date_dim dd 
WHERE dd.date_id = sf.date_id 
AND  dd.month_number = 1 
GROUP BY dd.month_number; 

AVGPERDAY MONTH_NUMBER 
-------------------------- 
113.785714  1 

結果是好的,但現在當我更改日期與dd.actual_date提示錯誤

SELECT sum(sf.ORDER_QNT)/EXTRACT(DAY FROM LAST_DAY(dd.actual_date)) as AVGPERDAY, 
     dd.month_number 
FROM sales_fact sf, 
     date_dim dd 
WHERE dd.date_id = sf.date_id 
AND  dd.month_number = 1 
GROUP BY dd.month_number; 


Error at Command Line : 1 Column : 53 
Error report - 
SQL Error: ORA-00979: not a GROUP BY expression 
00979. 00000 - "not a GROUP BY expression" 

誰能幫助?

回答

0
sum(sf.ORDER_QNT)/decode(round(max(dd.actual_date) - min(dd.actual_date)), 0, 1) 

你可以在你的查詢使用這個表達式(但結果可能是不一樣的用LAST_DAY)

或者你會做提取物(日從LAST_DAY(dd.actual_date))的一部分GROUP BY:

SELECT sum(sf.ORDER_QNT)/EXTRACT(DAY FROM LAST_DAY(dd.actual_date)) as AVGPERDAY, 
     dd.month_number 
FROM sales_fact sf, 
     date_dim dd 
WHERE dd.date_id = sf.date_id 
AND  dd.month_number = 1 
GROUP BY dd.month_number, EXTRACT(DAY FROM LAST_DAY(dd.actual_date)); 
+0

它工作順利。非常感謝:D – joeyy27 2014-10-09 08:49:05