sql日期函数

时间:2014-09-03 16:38:36

标签: sql oracle

我有一个' date1'值范围从01/01/1960到2014年9月14日的字段。我必须将此字段分组为

从今天开始的团队持续两个月进入

'1' (sept 2014, aug 2014)
(july 2014, june 2014) to '2'
(may 2014, apr 2014) to '3'
(mar 2014, feb 2014) to '4'
(jan 2014, dec 2013) to '5'
(nov 2013,oct 2013) to '6'
(<=sept 2013) to '7'

如何分组数据?

1 个答案:

答案 0 :(得分:1)

一个简单,冗长和静态的解决方案是这样的:

WITH t1 AS
(
    SELECT CASE WHEN date1 >= TO_DATE('1-AUG-2014') AND date1 < TO_DATE('1-OCT-2014') THEN 1 ELSE
           CASE WHEN date1 >= TO_DATE('1-JUN-2014') AND date1 < TO_DATE('1-AUG-2014') THEN 2 ELSE
           ... etc. END AS dategroup,
           othercolumns
    FROM Table1
)
SELECT dategroup FROM t1
GROUP BY dategroup;