我有一个包含以下示例信息的数据集:
ID DTE CNTR
1 20110102.0 2
1 20110204.0 3
1 20110103.0 5
2 20110205.0 6
2 20110301.0 7
2 20110302.0 3
如果我想按月对信息进行分组并对计数器求和,我猜的代码是这样的:
SELECT t.ID,
,SUM CASE(WHEN t.DTE between 20110101 and 20110131 then t.CNTR else 0) as Jan
,SUM CASE(WHEN t.DTE between 20110201 and 20110228 then t.CNTR else 0) as Feb
,SUM CASE(WHEN t.DTE between 20110301 and 20110331 then t.CNTR else 0) as Mar
FROM table t
GROUP BY t.ID
但是,有没有办法将这些信息聚合到另外两个名为“月”和“年”的列中并以这种方式进行分组,让我可以灵活地在许多不同的时间段内执行选择查询?