how to specify values to sort sql server

Asked

Viewed 505 times

3

Good afternoon!

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

How to specify to come that way?inserir a descrição da imagem aqui

  • place the structure of your tables and the code you are using, avoid putting the print of your screen.

  • I imagine that the "Description" should be in another table doing Join in your select, if this is the case, put a property to sort in that other table to avoid hard-code in the query

2 answers

2

I believe using order by with a CASE solve your problem:

ORDER BY
   CASE ColunaValor WHEN 'DIAMANTE' THEN 0 
                    WHEN 'OURO' THEN 1
                    WHEN 'PRATA' THEN 2
                    WHEN 'BRONZE' THEN 3
   END

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.