Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you give one direction for all columns in an order by?

Is it possible to provide the direction once for all columns in an order by statement?

i.e.

select *
from my_table
order by name desc, type desc

can you write the same thing using "desc" once?

Maybe something similar to this? (this doesn't work)

select *
from my_table
order by (name, type) desc
like image 724
Andy Avatar asked Jun 13 '11 18:06

Andy


2 Answers

You could use row_number for that:

select  *
from    my_table
order by 
        row_number() over (order by name, type) DESC

The final DESC will invert the row_number's order. So it'll flip ASC to DESC for both name and type.

like image 50
Andomar Avatar answered Oct 26 '22 17:10

Andomar


No. The SQL standard doesn't allow that.

Having said that, there may be some RDBMS that do support a syntax like that. I'm just not aware of any.

like image 41
Steve Prentice Avatar answered Oct 26 '22 17:10

Steve Prentice