开发者

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

开发者 https://www.devze.com 2023-03-12 08:50 出处:网络
Is it possible to provide the direction once for all columns in an order by statement? i.e. select * from my_table

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, ty开发者_开发百科pe) desc


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号