I’m a bit rusty with MySQL. I need some help figure out an order by.
I need it to be ordered by type, then by group with the highest group row date, and then each group ordered by date.
I need to make a query that will order a group rows by the highest date. I then need each group to be ordered by the highest date within that group. There is just one more thing. All rows a a type, but each group row has the same type.
Here is a visual representation of an unorganized table
| DATE | GROUP | TYPE |
-----------------------
| 2007 | 2 | 1 |
| 2008 | 3 | 2 |
| 2005 | 3 | 1 |
| 2004 | 2 | 1 |
| 2003 | 3 | 1 |
| 2012 | 3 | 2 |
| 2011 | 1 | 3 |
This organized:
| DATE | GROUP | TYPE |
-----------------------
| 2007 | 2 | 1 |
| 2004 | 2 | 1 |
| 2005 | 3 | 1 |
| 2003 | 3 | 1 |
| 2012 | 3 | 2 |
| 2008 | 3 | 2 |
| 2011 | 1 | 3 |
I’ve tried ordering it by type, group, than date, but this is inaccurate. I’m not entirely sure.
Here is a sample set of data:
| ID | SET | BLOCK | SHORTNAME | RELEASEDATE | SETTYPE |
----------------------------------------------------------------------------------
| 1 | Return to Ravnica | Return to Ravnica | rtr | 2012-09-29 | 1 |
| 2 | Gatecrash | Return to Ravnica | NULL | 2013-02-01 | 1 |
| 3 | Dragons Maze | Return to Ravnica | NULL | 2013-05-03 | 1 |
| 4 | Avacyn Restored | Innistrad | avr | 2012-05-04 | 1 |
| 5 | Dark Ascension | Innistrad | dka | 2012-02-03 | 1 |
| 6 | Innistrad | Innistrad | isd | 2011-09-30 | 1 |
| 7 | New Phyrexia | Scars of Mirrodin | nph | 2011-05-13 | 1 |
| 8 | Mirrodin Besieged | Scars of Mirrodin | mbs | 2011-02-04 | 1 |
| 9 | Scars of Mirrodin | Scars of Mirrodin | som | 2010-10-01 | 1 |
Group is block, type is settype, date is release date.
I need all block rows ordered by date within the block, each set of block rows ordered by highest date row within that block, and settype.
Derive the row set of highest dates per block from your table, then join it back to your table to use the highest dates in the ORDER BY clause: