过去12个月,按周分组

时间:2010-09-24 15:11:29

标签: group-by

我有一个表REGDATE列,一个注册日期(YYYY-MM-DD HH:MM:SS)。我想显示一个直方图(ExtJS),以了解用户注册的年份。我希望在过去的12个月内对当前日期和按周分组日期这样做。

任何提示?

4 个答案:

答案 0 :(得分:28)

PostgreSQL中的FWIW,Karaszi有一个有效的答案,但有一个更快的查询:

SELECT date_trunc('week', REGDATE) AS "Week" , count(*) AS "No. of users"
FROM <<TABLE>>
WHERE REGDATE > now() - interval '12 months' 
GROUP BY 1
ORDER BY 1;

我的基础是Ben Goodacre

的工作

答案 1 :(得分:22)

MySQL中的

SELECT COUNT(*), DATE_FORMAT(regdate, "%X%V") AS regweek FROM table GROUP BY regweek;

SELECT COUNT(*), YEARWEEK(NOW(), 2) as regweek FROM table GROUP BY regweek;
PostgreSQL中的

SELECT COUNT(*), EXTRACT(YEAR FROM regdate)::text ||  EXTRACT(WEEK FROM regdate)::text AS regweek FROM table GROUP BY regweek;

答案 2 :(得分:3)

也许这个?

select to_char(REGDATE,'WW') "Week number",
       count(*) "number of signups",
from YOUR_TABLE
where REGDATE > current_date-365
group by to_char(REGDATE,'WW')
order by to_char(REGDATE,'WW')

答案 3 :(得分:0)

提示:(SQL)

SELECT CONVERT (VARCHAR(7), REGDATE, 120) AS [RegistrationMonth]
FROM ...
GROUP BY CONVERT (VARCHAR(7), REGDATE, 120)
ORDER BY CONVERT (VARCHAR(7), REGDATE, 120)