I am making a SELECT
where together two tables: a stock balance table in stock and another table with purchase order.
I'm doing a comparison between balance of products in stock and balance in purchase orders, but the way I'm doing, SELECT
is only bringing products that have open purchase order balances in the system.
I need the SELECT
to bring not only the products that have balance in the purchase order, but also all products in the independent products table if you have a purchase order placed or not. Products that have no purchase order placed and SELECT
have a value of 0 in the column.
SELECT DISTINCT SB1.B1_DESC AS PRODUTO
, SUM(SB8.B8_SALDO) AS SALDO
, (SC7.C7_QUANT - SC7.C7_QUJE) AS [SALDO DE COMPRAS]
, SC7.C7_NUM AS PEDIDO
, SB1.B1_EMIN AS [SALDO MINIMO EM ESTOQUE]
FROM SB1010 AS SB1
INNER JOIN SB8010 AS SB8 WITH(NOLOCK) ON SB8.B8_PRODUTO = SB1.B1_COD
INNER JOIN SC7010 AS SC7 WITH(NOLOCK) ON SC7.C7_PRODUTO = SB1.B1_COD
WHERE ISB8.B8_LOCAL = '01'
AND SC7.C7_ENCER <> 'E'
AND SB1.D_E_L_E_T_ = ''
AND SB8.D_E_L_E_T_ = ''
AND SC7.D_E_L_E_T_ = ''
GROUP BY SB1.B1_DESC
, SB1.B1_EMIN
, SC7.C7_QUANT
, SC7.C7_QUJE
, SC7.C7_NUM
OBS : In this SELECT
I have a filter SC7.C7_ENCER
which means the purchase orders already completed.