我有一个users
表,用于在创建帐户时存储TIMESTAMP。我正在尝试编写一个查询,该查询将显示在给定时间段内按周(按周一开始)分组的新用户。导演将进入并选择报告日期范围,因此选择2个变量。他们应该看到显示所有在两个选定变量日期之间注册的新用户的结果,按周(周一周一开始)分组
变量: @StartRangeSelectDate @EndRangeSelectDate
用户表:
| id | TIMESTAMP
----------------------------------------------
| 1 | 2013-09-02
| 2 | 2013-09-08
| 3 | 2013-09-12
| 4 | 2013-09-27
结果应该是(当选择的开始日期类似于2013-09-01,结束日期是2013-09-29:
| YearWeek | # of New Users
----------------------------------------------
| 2013 36 | 2
| 2013 37 | 1
| 2013 38 | 0
| 2013 39 | 1
查询以显示日期范围内的新用户总数,但尚未在日期范围内按周分组:
SELECT COUNT(user.id) AS '# of New Users in Date Range'
FROM user
WHERE user.Inactive = 0
AND user.TimeStamp BETWEEN @StartRangeSelectDate AND @EndRangeSelectDate;
答案 0 :(得分:6)
SELECT yearweek(`TimeStamp`, 1) yw,
COUNT(id) AS '# of New Users in Date Range'
FROM user
WHERE Inactive = 0
AND TimeStamp BETWEEN @StartRangeSelectDate AND @EndRangeSelectDate
GROUP BY yw