Hi;
I work for a school system and I am wondering if anyone knows any MySQL query tricks that will allow queries to group by non-standard date groupings (without too much extra code). I have a lot of data tables with date fields and I would often like to do aggregate/group queries by school year (ie. starting (YEAR)-09-01 through (YEAR+1)-08-31).
So, given a data table with zillions of records and with fields like...
+------------+--------+----------+-------+------+------------+
| PROTOCOLID | TEST | SCHOOLID | GRADE | ROOM | TESTDATE |....
+------------+--------+----------+-------+------+------------+
| 423 | MATH | 185 | 1 | 0210 | 2006-05-17 |....
| 200 | ORF | 179 | 1 | 0016 | 2007-12-11 |....
| 201 | ORF | 179 | 1 | 0016 | 2005-04-11 |....
| 202 | IGDIS | 179 | 1 | 0016 | 2007-09-11 |....
| 203 | DIB | 179 | 1 | 0016 | 2004-03-11 |....
.....
a query could be built to output like:
+-----------+----+
| YEAR | N |
+-----------+----+
| 2001-2002 | 1 |
| 2002-2003 | 29 |
| 2003-2004 | 48 |
| 2004-2005 | 59 |
| 2005-2006 | 61 |
| 2006-2007 | 71 |
| 2007-2008 | 98 |
+-----------+----+
I am already using a dummy variable (a letter code for the school year) that I can use for such purposes, but I did not use it consistently in all my data tables, but I'd actually like to stop using that and rely on less kludgy programming.
...that, and I am always trying to live up to my philosphy of "The Lazy Man Will Find The Best Way."
It would be really slick if anyone could point out some MySQL function that would do such a thing "on the fly" rather than having to, say, loop through multiple queries with an array of start and end date filters.
Any ideas?
Thanks!