Prioritize line in sql queries

Asked

Viewed 137 times

1

In a Results with 2 fields exactly equal, how to do that only show rows that the priority column is marked as 1

id   |   origem   | destino  | daia_inicio  | data_fim   |  valor  | prioridade
01   |   Galeão   |  Barra   | 01/01/2018   | 31/12/2018 |  400,00 | 0
02   |   Galeão   |  Barra   | 10/01/2018   | 20/02/2018 | 1000,00 | 1
03   |   Rio      |  Barra   | 10/01/2018   | 20/02/2018 | 2000,00 | 0
04   |   São paulo|  Barra   | 10/01/2018   | 20/02/2018 | 4000,00 | 0
05   |   Brasilia |  Barra   | 10/01/2018   | 20/02/2018 | 8000,00 | 0

Expected result: ( Only show line 2 because you are priority marked )

02 Galeão Barra 10/02/2018 - 20/02/2018 1000,00

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

3

You will have to use the clause WHERE in your query to filter the results.

Example:

SELECT * FROM nome_da_tabela WHERE prioridade = 1

See the documentation of SELECT

  • My bad, already removed and changed an "extra" :) min 6 chars -.-

  • Now yes! Thank you!

  • Partner, that doesn’t solve my case. so only shows lines with priority = 1 ,, more and the rest that has no priority will no longer be shown... think the following.... in a list of 10 lines where 2 lines are equal, plus one of the same has priority.. then the query should return 9 because between 2 repeated it should exclude the one that had no priority

  • 3

    Then enter exactly what you need in the question field, brother. The way you wrote: "how to do only show rows that the priority column is marked as 1", me and other people understand that your problem was this. Give more details...

1

You can compare the table with itself to get the highest priority:

SELECT *
  FROM tabela t
 WHERE t.prioridade = (SELECT MAX(t2.prioridade)
                         FROM tabela t2
                        WHERE t2.origem = t.origem
                          AND t2.destino = t.destino)

Browser other questions tagged

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