I have three tables: PRODUTOS
, PEDIDO
and ESTOQUE
, and I want to list all the inventory going by order and by product, as I show below:
products
codigo tipo medida 3020 | unico | 3 3021 | unico | 5 3022 | unico | 7 3023 | unico | 3
request
codigo qdtpedido 3020 | 10 3021 | 20 3022 | 10 3020 | 5 3021 | 3 3022 | 5
stock
codigo qdtestoque 3020 | 200 3021 | 150 3022 | 50 3023 | 120
Select result
codigo qtdestoque tipo medida qtdpedido 3020 | 185 | unico | 3 | 15 | 3021 | 127 | unico | 5 | 23 | 3022 | 35 | unico | 7 | 15 | 3023 | 120 | unico | 3 | 0 |
What I'm trying to do:
SELECT e.*,
(SELECT sum(qtdpedido) from pedido p where p.modelo = e.codigo) as qtdpedido,
(SELECT tipo from produtos m where m.codigo = e.codigo) as tipo
from estoque e ORDER BY codigo ASC;
As a result I need to list Inventory (which contains all products), bringing the field "type" and "measure" that comes from the Products table and adding the "orders" for that code.
Could anyone help me?