如何从结果集中按月计算日期条目

时间:2013-09-13 09:32:47

标签: java sql

我正在建立一个数据库连接并运行一个sql查询,它返回一个带有单列java.util.date类型的rsultset,现在在列中我有条目,

2012-03-20 13:50:27.278
2011-08-11 00:30:31.121
2011-08-11 00:35:48.726
2011-08-11 01:18:41.712
2011-08-11 01:20:20.403
2011-08-11 01:22:49.588
2011-08-11 01:25:51.451
2013-02-14 19:39:06.837
2011-08-11 14:46:01.376
2011-08-13 05:43:20.744
2011-08-13 05:46:05.477
2011-08-16 08:07:56.551
2011-08-18 13:21:55.534
2011-08-18 13:23:13.961
2011-08-18 13:33:58.683
2011-08-18 13:36:29.199
2011-08-23 12:02:36.086
2012-12-31 13:49:21.999
2013-02-28 17:49:18.938
2013-05-04 19:17:39.512
2011-09-02 20:17:33.023
2011-09-05 21:50:18.495
2011-09-09 21:14:26
2011-09-09 21:42:48.984
2011-09-09 21:59:27.321

我想计算每个月的参赛人数?有什么帮助吗?

2 个答案:

答案 0 :(得分:5)

为什么不在月份修改SELECT使用GROUP BY

In PostgreSQL你可以这样做:

  SELECT count(*), y, m
    FROM t
  -- AND more conditions
GROUP BY extract(year from timecolumn) AS y,
         extract(month from timecolumn) AS m;  

答案 1 :(得分:0)

使用SimpleDateFormat将日期格式化为文本,仅输出年份和月份,然后计算出现次数。

如何做到后者是非常明显的,例如使用一个Set,其中键的格式为“yyyy-mm”