V
V
valek972020-11-26 20:41:08
SQL
valek97, 2020-11-26 20:41:08

How to display the warehouse with the largest number of goods and a unique product?

5fbfe74dc152d559353263.png
5fbfe7532f636540745294.png
The essence of the task. Display those products (product.name), the balance of which in all warehouses in total exceeds 100 units (goods.quantity), indicating the warehouse (warehouse.namme) where the largest quantity is located.

select distinct product.name, warehouse.name, Sum(goods.quantity) AS "Count"
from product, warehouse, goods
where (goods.productId = product.id and goods.warehouseId = warehouse.id and goods.quantity >100  )
group by warehouse.name, product.name
order by warehouse.name asc, product.name asc

I have a problem with how to remove unnecessary warehouses, take a warehouse where the largest number of goods.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur-Salo, 2020-11-27
@Artur-Salo

select distinct p.name, w.name, sum(g.quantity) as "count"
from product p, warehouse w, goods g
where (goods.productId = product.id and goods.warehouseId = warehouse.id
and p.id in
(select productid from
(select productid, sum(quantity) from goods group by productid having sum(quantity) > 100) sq1
) sq2
group by p.name, w.name
order by sum(g.quantity) desc
Using two subqueries .Crooked, but should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question