1

like i have 1000 order entry in each entry i have multiple products with sold quantity for top selling products.heaare my challange is i need return the top sold products example:

order entry  : product id  :sold quantity;;;;
entry 1     : productA    :20 
            :productB     :10
             :productC     :5
entry 2      :productB     :5
             :productc      :20

how can get the top selling product.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786

1 Answers1

0

If I understand correctly, this is aggregation with order by:

select product_id, sum(quantity) as total_quantity
from t
group by product_id
order by total_quantity desc
limit 10;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • select {product}, sum{quantity} as totalquantity from {orderentry} groupby {product} order by totalquantity desc limit 10. i tries this query but this is taking lot of time . hoiw can i optimise this query – Mani Sankar Sep 29 '20 at 22:34
  • @ManiSankar . . . I think you asked a new question about that topic. Unaccepted this answer is not going to get you close to a solution. – Gordon Linoff Sep 29 '20 at 22:48
  • select {p.pk} from { order as o join OrderStatus as os on {os.pk}={o.status} join orderentry as oe on{oe.order}={o.pk} join product as p on {oe.product}={p.pk} } where {os.code}='COMPLETED' AND {o.date}>'2020-08-16 00:00:00.000' AND{o.date}<'2020-09-30 00:00:00.000' group by{p.pk}order by count({oe.pk}) desc limit 10 i wrote this query but i sttruck with how to get all roducts inseted of only pk – Mani Sankar Oct 02 '20 at 22:19