Can you give one direction for all columns in order?

Can I specify the direction for all columns in order?

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 does not work)

 select * from my_table order by (name, type) desc 
+6
source share
2 answers

Not. The SQL standard does not allow this.

Having said that, there may be some RDBMSs that support this syntax. I just don’t know anything.

+4
source

You can use row_number to do this:

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

In the last DESC, the row_number order will be inverted. Thus, it will flip ASC to DESC for name and type.

+7
source

Source: https://habr.com/ru/post/890430/


All Articles