回答

收藏

一年中如何查询?GROUP BY

技术问答 技术问答 532 人阅读 | 0 人回复 | 2023-09-14

我正在使用Oracle SQL Developer。我基本上有一张包含各列的图片表:5 m% F/ h& B. T
[DATE_CREATED(日期),NUM_of_PICTURES(int)]
. Z" ^1 Z! N' Z$ y9 S3 E$ u0 o如果我执行select *,输出类似于以下内容:2 G, X6 o! _7 h! G2 J! C" B
01-May-12    1202-May-12    1503-May-12    09......01-Jun-12    20...etc.我试图总结这些图片的总和MONTHLY而非数字DAILY中。
: d4 h9 {: _9 Z1 }5 A我试着做类似的事:
( M8 [9 g( c7 C; I& |8 Xselect Month(DATE_CREATED),sum(Num_of_Pictures))from pictures_tablegroup by Month(DATE_CREATED);输出错误:
) n7 P- Z5 e- N& d. u$ _/ ~2 TORA-00904: "MONTH": invalid identifier00904. 00000 -  "%s: invalid identifier"*Cause:    *Action:Error at Line: 5 Column: 9我的月功能错了吗?4 }4 m; ~* o/ L7 U7 Q* k) D
                                                                0 Q7 ^' Q8 f# {, L7 p% |
    解决方案:                                                                9 {- G: ]  i0 E  p
                                                                我倾向于在输出中包括年份。; `: r3 R7 g& E
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):* q4 g) u) ^* m1 x8 w
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;记住顺序,因为你可能希望这些顺序,不能保证分组后返回的顺序。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则