How to know which supplier provides more products by returning the supplier’s name?

Asked

Viewed 33 times

1

First of all, this is the code that I’m trying to sum up the products of each lab and display

select l.razaosocial,  sum(p.quantidade)   from produto p
inner join laboratorio l on p.fk_laboratorio_id = l.id_laboratorio;

Only the result is the SUM of all products regardless of which registered supplier, and returning the first registered supplier.

I wanted something that would return which supplier most supplies products, with the quantity and his name in the case, social reason....

1 answer

0

You can use the command Group By to bring the results grouped by supplier and a Order by to bring orderly.

So for example

Select l.razaosocial AS Fornecedor, Sum(p.quantidade) AS Qtd
FROM produto p
INNER JOIN laboratorio l ON p.fk_laboratorio_id = l.id_laboratorio
GROUP BY Fornecedor
ORDER BY Qtd DESC;

Browser other questions tagged

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