长得丑但是想的美 发表于 2023-9-14 12:11:30

一年中如何查询?GROUP BY

我正在使用Oracle SQL Developer。我基本上有一张包含各列的图片表:

如果我执行select *,输出类似于以下内容:
01-May-12    1202-May-12    1503-May-12    09......01-Jun-12    20...etc.我试图总结这些图片的总和MONTHLY而非数字DAILY中。
我试着做类似的事:
select Month(DATE_CREATED),sum(Num_of_Pictures))from pictures_tablegroup by Month(DATE_CREATED);输出错误:
ORA-00904: "MONTH": invalid identifier00904. 00000 -"%s: invalid identifier"*Cause:    *Action:Error at Line: 5 Column: 9我的月功能错了吗?
                                                               
    解决方案:                                                               
                                                                我倾向于在输出中包括年份。
select to_char(DATE_CREATED,'YYYY-MM'),sum(Num_of_Pictures)from pictures_tablegroup 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_tablegroup by extract(year from date_created),extract(month from date_created)order by yr,mon;记住顺序,因为你可能希望这些顺序,不能保证分组后返回的顺序。
页: [1]
查看完整版本: 一年中如何查询?GROUP BY