需要快速帮助 我有这个小项目,我有这个报告,总共有订单和订单的日期(这实际上是我的另一个问题的延续) 所以这就是我一直在做的事情
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 :(得分:0)
sum(sf.ORDER_QNT) / decode(round(max(dd.actual_date) - min(dd.actual_date)), 0, 1)
您可以在查询中使用此表达式(但结果可能与LAST_DAY不同)
或者您可以将EXTRACT(DAST FROM 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));