Sort the largest on request

Asked

Viewed 216 times

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:

inserir a descrição da imagem aqui

How do I bring the 10 customers who got the biggest discounts per order without the same discount amount being repeated.

thank you in advance...

2 answers

0

There’s your solution, my friend.

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
  • So I tried with this command and the result was all discounts with values 0 (Zero).

  • I tried too... select cliente_id, discount, pedidos_details.pedido_id, cliente_type from pedidos_details Inner Join requests on request_details.pedido_id = orders.pedido_id ORDER BY discount desc LIMIT 10;

  • I forgot to put DESC, which brings the data from the largest to the smallest. Also has the ASC option that brings the data from the smallest to the largest.

  • and the result brought 0.10 discounts on all discounts, increased the LIMIT to 100 and all discounts are 0.10

  • So with DESC brings all discounts equal to 0.10

  • So, but in this case it is ordering the results from the highest to the lowest, if you remove the LIMIT clause 100, it will bring all the results ordered by the discount, right ?

  • No, if I take the LIMIT and leave only the DESC, all the discount fields comes with value 0.10

  • But you have all these discounts with 0.10 ?

  • So... rsrsrrss. no... if I put the group by discount it brings the discounts in descending order from 0.10 to 0.00

Show 5 more comments

0

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


For you to bring the 10 customers who got the biggest discounts per order, without the same discount amount being repeated. You must use the clause GROUP BY, which groups lines based on similarities between them.

To order these values and take the largest per request you must use the clause ORDER BY. For when we need the result of our query to a table to be provided in a specific order, according to a certain criterion, we should use the clause ORDER BY which, as its name says, considers a certain order to return the data of a query.

Browser other questions tagged

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