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
MySQL - Group results by weeks in a date range
-
30-06-2022 - |
Question
I have a users
table that stores TIMESTAMP, when their account was created. I'm trying to write a query that will show the new users that signed up grouped by week (Starting Mondays), in a given period of time. A director will go in and pick the report date ranges, thus the 2 variables. They should see results that display all new users that have signed up between the 2 selected variable dates, grouped by Week (Monday start of the Week)
Variables: @StartRangeSelectDate @EndRangeSelectDate
User Table:
| id | TIMESTAMP
----------------------------------------------
| 1 | 2013-09-02
| 2 | 2013-09-08
| 3 | 2013-09-12
| 4 | 2013-09-27
Result Should Be (when selected start date is something like 2013-09-01 and end date is 2013-09-29:
| YearWeek | # of New Users
----------------------------------------------
| 2013 36 | 2
| 2013 37 | 1
| 2013 38 | 0
| 2013 39 | 1
Query to Display Total New Users in Date Range, but just not yet grouped by weeks in the date range:
SELECT COUNT(user.id) AS '# of New Users in Date Range'
FROM user
WHERE user.Inactive = 0
AND user.TimeStamp BETWEEN @StartRangeSelectDate AND @EndRangeSelectDate;
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow