how to do a query by ordering a rising column and a descending column

0

I need to do a query that looks for a growing column and in the same query another column in descending for example in the result below:

item   | quantidade
carro  |    400
carro  |    340
dado   |    240
disco  |    180
disco  |    120
faca   |    89
faca   |    59

I would like to see the result follow my query below:

SELECT * FROM qe.etiquetas_recortadas_aoi ignore index(PRIMARY) 
where line = 'AOI-1' 
and brand = 'NBK'  
and material_name_crop <> 'Borrada_1'  
order by qty_pass desc, material_name asc;
    
asked by anonymous 25.04.2018 / 12:40

1 answer

2

As you are sorting:

ORDER BY qty_pass DESC, material_name ASC

You are sorting first by qty_pass , so if you do not have equal values in qty_pass , then you will never sort material_name .

Example:

quantidade | item
    400    | carro
    340    | disco
    240    | dado
    180    | carro
    120    | disco
     89    | faca
     59    | faca

Reversing orders:

ORDER BY material_name ASC, qty_pass DESC

In this way, you will sort first by material_name and then qty_pass .

Example:

item   | quantidade
carro  |    400
carro  |    180
dado   |    240
disco  |    340
disco  |    120
faca   |     89
faca   |     59

In%%, the order of factors changes the result.

ORDER BY condicao1, condicao2 DESC, condicao3 ASC, ...
When you have equal values in the first condition (already ordered), it will sort the second, and so on, in the given sequence.

When ORDER BY or ASC is not determined, by default, it will sort DESC     

25.04.2018 / 13:36