Re: How can a group by clause be modified so the end weeks of the year won't be split?

From: Bob Badour <>
Date: Wed, 28 Jan 2009 18:11:01 -0400
Message-ID: <4980d7f7$0$5498$>

Ted Byers wrote:
> I have a SELECT statement similar to the following:
> SELECT YEAR(transaction_date) AS y,WEEK(transaction_date) AS w, COUNT
> (*) AS c FROM transaction_data_view WHERE mid = 300
> GROUP BY YEAR(transaction_date),WEEK(transaction_date)
> ORDER BY YEAR(transaction_date),WEEK(transaction_date);
> The ONLY problem, here, is that if New Years day occurs in the middle
> of the week, that week's data will be split at the first second of the
> New Year. Of course, my real select statement is much more complex
> than this, involving joins of both tables and views (and it is quick),
> but this suffices to make the only remaining problem obvious.
> If it matters, this is being done in MySQL.
> So, how, then, can I fix the GROUP BY and ORDER BY clauses?
> Thanks,
> Ted

Normalize the date for calculating the year. Instead of grouping by the YEAR of the current date, group by the YEAR of the first week of the year that contains the current date. I will leave as an exercise how to calculate the first week from the current date using the YEAR and WEEK functions above plus whatever date arithmetic operators MySQL uses. Received on Wed Jan 28 2009 - 16:11:01 CST

Original text of this message