Two steps to this:
First, you will need a week truncation operation — this will take your DATETIME element and return at midnight on the previous Sunday (if your business rule says that the week starts on Sunday).
This becomes a suitable GROUP BY element. Neke () / YEAR () hacking is not suitable for this. It really makes a mess in the last / first week of each year.
In my experience, this expression will do the week truncation trick for you, Sunday is Saturday,
FROM_DAYS(TO_DAYS(TIMESTAMP) -MOD(TO_DAYS(TIMESTAMP) -1, 7))
To get Monday through Sunday use this expression.
FROM_DAYS(TO_DAYS(TIMESTAMP) -MOD(TO_DAYS(TIMESTAMP) -2, 7))
So you can do it.
SELECT COUNT(whatever), SUM(whatelse), FROM_DAYS(TO_DAYS(event_time) -MOD(TO_DAYS(event_time) -1, 7)) as WEEKSTART, FROM TABLE GROUP BY FROM_DAYS(TO_DAYS(event_time) -MOD(TO_DAYS(event_time) -1, 7))
How to group by week in MySQL?
Second, you need to add six days to this truncated date so that you can display the last day of each week along with the first day.
This is a good way to do this, with a subquery
SELECT whats, elses, weekstart, weekstart + INTERVAL 6 DAY AS weekend FROM ( SELECT COUNT(whatever) AS whats, SUM(whatelse) AS elses, FROM_DAYS(TO_DAYS(event_time) -MOD(TO_DAYS(event_time) -1, 7)) AS weekstart, FROM TABLE GROUP BY FROM_DAYS(TO_DAYS(event_time) -MOD(TO_DAYS(event_time) -1, 7)) ) AS summary ORDER BY weekstart
Do a lot of this? I suggest you create a saved function TRUNC_WEEK .