0
I have three tables: PRODUTOS
, PEDIDO
and ESTOQUE
, and I want to list all the stock by order and by products, as shown 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
stockpile
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 Stock (in which it contains all products), bringing the "type" and "measure" field that comes from the Products table and adding the "qtdpedidos" referring to that code.
Could someone help me?
What you need is to use clauses INNER JOIN, LEFT JOIN and RIGHT JOIN https://www.devmedia.com.br/clausulas-inner-join-left-join-e-right-join-no-sql-server/18930
– Valéria Pereira
Your tables are a little confused... List the table
produtos
with theestoque
but you don’t have a tableitens_pedidos
? There are more fields you didn’t report?– Matheus Ribeiro