Sort by * where *

Sorry my English is not the best.

Is it possible to make "where" in "order by".

Example:

ORDER BY SUM(points), SUM(points) WHERE type = 1, SUM(goal), SUM(goal) WHERE type = 1 

So, first we sort by points.

If someone has the same number of SUM(points) , then sort by SUM(points) where type = 1 .

If someone has the same number of SUM(points) and the same number of SUM(points) where type = 1 , then we sort after SUM(goal) .

If someone has the same thing in all 3, then he should sort by SUM(goal) where type = 1 .

+4
source share
2 answers
 SUM(CASE WHEN type=1 THEN points ELSE 0 END) 

and etc.

You must, of course, add DESC to all the conditions of the order, if you want "better" first;)

And sqlFiddle

+4
source
 ORDER BY SUM(points), SUM(CASE WHEN type = 1 THEN points ELSE 0 END), SUM(goal), SUM(CASE WHEN type = 1 THEN goal ELSE 0 END) 
0
source

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


All Articles