Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing a secondary sort by year in a SQL query

Tags:

sql

ms-access

So I have a table that with ID values and dates that looks something like this:

ID           Date
0001         1/1/2012
0002         1/2/2010
0002         1/2/2011
0001         1/1/2011
0001         1/1/2010
0002         1/2/2012

Basically, the ID values are unique to that year only - they reset the subsequent year.

I want to be able to sort by ID values and by dates, but I want to do the sorting so that the values are ordered by year. Just a regular sort of ID with a secondary date sort yields this:

ID           Date
0001         1/1/2010
0001         1/1/2011
0001         1/1/2012
0002         1/2/2010
0002         1/2/2011
0002         1/2/2012

But I would like a query that generates a table that looks like this:

ID           Date
0001         1/1/2010
0002         1/2/2010
0001         1/1/2011
0002         1/2/2011
0001         1/1/2012
0002         1/2/2012

Is this possible?

like image 510
user3642531 Avatar asked Aug 27 '14 14:08

user3642531


1 Answers

How about this:

order by year(date), id
like image 158
Gordon Linoff Avatar answered Nov 08 '22 23:11

Gordon Linoff