SQL query using order by filtering by log

Asked

Viewed 46 times

1

I have the table:

| cod | hora  | produto | Amostra | Min | Max |
|-----|-------|---------|---------|-----|-----|
|   1 | 08:00 |  96722  |   1     | 100 | 200 |
|   2 | 08:23 |  96721  |   1     | 90  | 90  |
|   3 | 08:50 |  96722  |   2     | 100 | 100 |
|   4 | 09:50 |  96722  |   2     | 100 | 200 |
|   5 | 08:30 |  96721  |   2     | 90  | 90  |
|   6 | 08:28 |  96721  |   1     | 12  | 25  |

I want you to list me like this:

| cod | hora  | produto | Amostra | Min | Max |
|-----|-------|---------|---------|-----|-----|
|   2 | 08:23 |  96721  |   1     | 90  | 90  |
|   5 | 08:30 |  96721  |   2     | 90  | 90  |
|   6 | 08:28 |  96721  |   1     | 12  | 25  |
|   1 | 08:00 |  96722  |   1     | 100 | 200 |
|   4 | 09:50 |  96722  |   2     | 100 | 200 |
|   3 | 08:50 |  96722  |   2     | 100 | 100 |

I’m trying to do it that way:

select * from tb_tabela
order by
tb_tabela.produto,
tb_tabela.amostra,
tb_tabela.hora

He’s returning me wrong, as per the bass:

| cod | hora  | produto | Amostra | Min | Max |
|-----|-------|---------|---------|-----|-----|
|   2 | 08:23 |  96721  |   1     | 90  | 90  |
|   6 | 08:28 |  96721  |   1     | 12  | 25  | //errado
|   5 | 08:30 |  96721  |   2     | 90  | 90  |
|   1 | 08:00 |  96722  |   1     | 100 | 200 |
|   3 | 08:50 |  96722  |   2     | 100 | 100 | //errado
|   4 | 09:50 |  96722  |   2     | 100 | 200 |
  • Hi Tiago, I’m not an expert in SQL or anything but the way you want I think there is no way to do... How to run an order by to come 1, 2 ,1 in the Sample column? No other column to sort? Type using Min/Max?

  • Can’t use Min/Max because they can vary.

  • What sense would it have in ordering this way? in your example none of the fields has a valid order set to be made.

1 answer

0


test this query

select * from tb_tabela
order by
tb_tabela.produto ASC,
tb_tabela.Max DESC,
tb_tabela.Min DESC

should work ( edited )

  • did not work, gave the same problem

  • I edited the test query now

  • Did it work? I forgot to insert max in the initial query

  • worked, thanks

  • good da at least one up ai rsss

Browser other questions tagged

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