回答

收藏

一年中如何查询?GROUP BY

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

我正在使用Oracle SQL Developer。我基本上有一张包含各列的图片表:, l; X  T1 u' X# E, r3 l& |
[DATE_CREATED(日期),NUM_of_PICTURES(int)]  g& y7 {8 M0 [8 D4 s
如果我执行select *,输出类似于以下内容:# x3 Q. ^5 @8 S! o
01-May-12    1202-May-12    1503-May-12    09......01-Jun-12    20...etc.我试图总结这些图片的总和MONTHLY而非数字DAILY中。
2 @& [/ L* A% E7 v5 s我试着做类似的事:& d; ?. S( k! D
select Month(DATE_CREATED),sum(Num_of_Pictures))from pictures_tablegroup by Month(DATE_CREATED);输出错误:- |3 k% x  r5 b' _
ORA-00904: "MONTH": invalid identifier00904. 00000 -  "%s: invalid identifier"*Cause:    *Action:Error at Line: 5 Column: 9我的月功能错了吗?
+ J, p5 Y3 ~$ S& Y/ T                                                                6 K# t  i: e; ~$ Y
    解决方案:                                                               
. C2 o" M" @; ]" a                                                                我倾向于在输出中包括年份。
, Y, V8 I- O8 U/ [( tselect to_char(DATE_CREATED,'YYYY-MM'),sum(Num_of_Pictures)from pictures_tablegroup by to_char(DATE_CREATED,'YYYY-MM')order by 1另一种方法(更标准)SQL):
( u: Y$ Z! q' U1 j) s' Tselect 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;记住顺序,因为你可能希望这些顺序,不能保证分组后返回的顺序。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则