我正在使用Oracle SQL Developer。 我基本上有一个容纳列的图片表:
[DATE_CREATED(date), NUM_of_PICTURES(int)] 。
如果我做一个选择*,我将得到一个类似的输出:
01-May-12 12
02-May-12 15
03-May-12 09
...
...
01-Jun-12 20
...
etc.
我试图将这些图片的总和汇总成每月的数字,而不是每天的数字。
我已经尝试做了一些事情:
select Month(DATE_CREATED), sum(Num_of_Pictures))
from pictures_table
group by Month(DATE_CREATED);
这输出了一个错误:
ORA-00904: "MONTH": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 5 Column: 9
我的Month函数是否有误?
我倾向于在输出中包括年份。 一种方法:
select to_char(DATE_CREATED, 'YYYY-MM'), sum(Num_of_Pictures)
from pictures_table
group by to_char(DATE_CREATED, 'YYYY-MM')
order by 1
另一种方法(更标准的SQL):
select extract(year from date_created) as yr, extract(month from date_created) as mon,
sum(Num_of_Pictures)
from pictures_table
group by extract(year from date_created), extract(month from date_created)
order by yr, mon;
请记住order by,因为你可能希望这些数据是按顺序排列的,而且不能保证在group by后返回的行的顺序。
对于甲骨文:
select EXTRACT(month from DATE_CREATED), sum(Num_of_Pictures)
from pictures_table
group by EXTRACT(month from DATE_CREATED);