V
V
Vitaly Rybchenko2018-03-19 13:18:50
SQL Server
Vitaly Rybchenko, 2018-03-19 13:18:50

Is it possible to sort with conditions?

Hello!
Is it possible to sort with conditions so that if there is a value equal to A, ASC is sorted, and if B is sorted DESC?
For example something like this:

select Id, Category, Name
from dbo.Products
order by Category, case when Category = 'new' then Id desc else id asc end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2018-03-19
@biovr

select Id, Category, Name
from dbo.Products
order by Category, case when Category = 'new' then Id * -1 else id end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question