Order the highest per order

0

Good evening, I'm using the following command:

select 
   cliente_id, 
   desconto,
   pedidos_detalhes.pedido_id, 
   cliente_tipo 
from 
   pedidos_detalhes
inner join 
   pedidos on pedidos_detalhes.pedido_id = pedidos.pedido_id;

The result is this:

How do I get the 10 customers who got the highest discounts per order, without the same discount amount being repeated.

Thank you very much ...

    
asked by anonymous 21.09.2016 / 01:30

2 answers

0

Here's your friendly solution.

select 
    cliente_id, 
    desconto, 
    pedidos_detalhes.pedido_id, 
    cliente_tipo 
from 
    pedidos_detalhes 
inner join 
    pedidos on pedidos_detalhes.pedido_id = pedidos.pedido_id 
GROUP BY 
    desconto
ORDER BY
    desconto
DESC
LIMIT 10
    
21.09.2016 / 01:46
0

Complementing the answer, so you understand the meaning of the clauses.

For you to bring the 10 customers who have obtained the highest discounts per order, without the same discount value being repeated. You should use the clause GROUP BY , which groups lines based on similarities between them.

To sort these values and get the highest per request you must use the ORDER BY clause. For when we need the result of our query to a table be given in a specific order, according to a certain criterion, we must use the ORDER BY clause which, as the name itself says, considers a certain order to return the data from a query.

    
21.09.2016 / 04:37