Skip to content
Advertisement

Order by multiple columns not working as expected

I have this sql :

GROUP BY dc.year, dc.month, d.user_id
ORDER BY dc.year DESC, d.user_id ASC

The result is :

UserId         Month            Year
 1               1              2020
 1               4              2020
 1               2              2020
 2               5              2019
 2               1              2019

Expected result :

UserId         Month            Year
 1               1              2020
 1               2              2020
 1               4              2020
 2               1              2019
 2               5              2019  

But I want to order and by month not only by year. I tried like this ORDER BY dc.year DESC, dc.month ASC, d. user_id ASC, but not working as expected because I want to liste each user order by year desc and by month asc. Can you help me please ?

Advertisement

Answer

Looks like you don’t need user_id in the ORDER BY clause.

Try this:

ORDER BY dc.year DESC, d.user_id, dc.month 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement