how to specify values to sort sql server

3

Good afternoon!

Well, I have the following query below, however, I want to sort by specifying some records. Attached for example, is the generated query and I want the Value column to look like this: Diamond Gold Bronze Silver

How do you specify it to come this way?

    
asked by anonymous 12.07.2017 / 19:21

2 answers

1

You can create an order column with CASE :

SELECT CASE t.campo
         WHEN 'DIAMANTE' THEN 0
         WHEN 'OURO' THEN 1
         WHEN 'PRATA' THEN 2
         WHEN 'BRONZE' THEN 3
       END AS ordem
  FROM tabela t
 ORDER BY 1
    
12.07.2017 / 19:37
2

I believe that using order by with CASE solves your problem:

ORDER BY
   CASE ColunaValor WHEN 'DIAMANTE' THEN 0 
                    WHEN 'OURO' THEN 1
                    WHEN 'PRATA' THEN 2
                    WHEN 'BRONZE' THEN 3
   END
    
12.07.2017 / 19:36