日期:2014-05-16  浏览次数:20452 次

按年或月份Group by的语句怎样写?
这个问题我也碰到了,花了一个上午查了一些资料,终于摸索解决啦。

? ? 解决这个问题主要用到了一个函数:extract。其主要的语法如下:

? ?EXTRACT (component_name, FROM {datetime | interval})

Datetime component names for use with EXTRACT

Component name: Return datatype
---------------------------------------------------
YEAR: NUMBER

MONTH: NUMBER

DAY: NUMBER

HOUR: NUMBER

MINUTE: NUMBER

SECOND: NUMBER

TIMEZONE_HOUR: NUMBER

TIMEZONE_MINUTE: NUMBER

TIMEZONE_REGION: VARCHAR2

TIMEZONE_ABBR: VARCHAR2

---------------------------


? ? 基于此,再结合group by 语句,可以实现数据按年月日分组汇总,例句如下:

? ? select extract(year from tdate), extract(month from tdate), extract(day from tdate), count(*) from t_t group by extract(year from tdate), extract(month from tdate), extract(day from tdate);