Sort a Select by criteria outside the database

Asked

Viewed 69 times

5

I would like to create a SELECT with the personal relationship here of my company.

SELECT função, nome FROM responsavel;

So far so good. But as ordered by the Manager, passing by the Incumbent, Supervisor until General Services Assistant, in this order?

THAT IS TO SAY:

GERENTE, FULANO;
ENCARREGADO, CICLANO;
SUPERVISOR, BETÂNIO;
AUX. DE SERV. GERAIS, REGÂNIO;

In this order.

  • 2

    @Robertodecampos, I do not think it is a duplicate saw, it seems what he wants is something like an Ordination according to a hierarchy of the company, as if, the "Manager" was more important and came first, then "In Charge". In the other case of the link, the guy wanted only the city "Boston" to be in first. At least that’s what I understood, and if he was going to use the same idea as the link response, I guess I would have to do !=for everyone, it would be weird. But this is what I think kkk

  • I edited my answer, I believe that now this in agreement :)

  • You’re right @Richardwillian, I’ll take my vote as duplicate.

2 answers

5

Look at this solution with case:

SELECT funcao  nome FROM responsavel
    order by case funcao 
                when "GERENTE" THEN 1
                when "ENCARREGADO" THEN 2
                when "SUPERVISOR" THEN 3
                when "AUX. DE SERV." THEN 4 
                else  99999
            end;
  • https://answall.com/a/34358/4927

  • in case he would not need, because he just wanted one of the fields to come first, in yours you want to pass the order of values.

  • Thanks @Marcianomachado! Helped me a lot

  • You’re right @Marcianomachado, I’ve given my +1

1

Perhaps the best way to solve your problem would be to add a new column as one of Importância Cargoor Hierarquia Ofício, the name does not matter so much, but rather the application.

By adding this new column, you could make a simple ORDER BY, causing your Queryget cleaner.

       FUNCAO       |        NOME      | HIERARQUIA_OFICIO|
GERENTE             | FULANO           |        1         |
ENCARREGADO SETOR 1 | CICLANO DA MATA  |        2         |
ENCARREGADO SETOR 2 | CICLANO DA SILVA |        2         |
SUPERVISOR          | BETÂNIO          |        3         |
AUX. DE SERV. GERAIS| REGÂNIO          |        4         |

SELECT FUNCAO, NOME FROM RESPONSAVEL
ORDER BY HIERARQUIA_OFICIO;

Of course, I don’t know how hard it will be to add a new column, sometimes it is necessary to change A lot thing in your system, there really doesn’t pay. But if it does, maybe this is the best way.

Browser other questions tagged

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