S
S
Sergey Prishchenko2017-08-23 15:42:53
PostgreSQL
Sergey Prishchenko, 2017-08-23 15:42:53

How to find products in stock?

There are 3 tables
f0bafa4e299645a988e87a1ca82cc14b.png
When a new product arrives, a new record is created in the receipt table with the indication good_id. If the goods arrived in a quantity of 4 pcs, there will be 4 new entries. When a product is sold, a new record Question
is created in the sale table .
How to find products in stock and with quantity in one query

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gill-sama, 2017-08-23
@jonkofee

select g.id, g.name, g.description, g.bar_code, s.count as total_sales, r.count as total_receipts, r.count-s.count as total  from good g join (select good_id, count(*) from receipt group by good_id) r on g.id = r.good_id
join (select good_id, count(*) from sale group by good_id) s on g.id = s.good_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question