How to compare two values โ€‹โ€‹of sql query

I have two tables StockOutward and a product outward. I need to get a sum (qty) that is not equal to the sum (qty) stockOutward ..

StockOutward

Id ProductId Qty Location Orderid

1 7 2 2 38
2 8 1 2 38
3 7 1 2 38

ProductOutward

Id ProductId Qty Location Orderid

1 7 12 2 38
2 8 1 2 38

I need to withdraw from stock as ProductId 7

I used the following query

Select
    sum(qty) as Qty,ProductId 
from 
    StockOutward 
where 
    Orderid='38' 
group by 
    ProductId 

Union

Select 
    sum(qty) as Qty,
    ProductId 
from 
    ProductOutward 
where 
    Orderid='38' 
group by 
    ProductId

      

+3


source to share


2 answers


You can use JOIN

and filter for inequality:



SELECT
    s.ProductId
FROM (
    SELECT
        ProductId,
        SumQty = SUM(Qty)
    FROM StockOutward
    GROUP BY ProductId
)s
INNER JOIN (
    SELECT
        ProductId,
        SumQty = SUM(Qty)
    FROM ProductOutward
    GROUP BY ProductId
)p
    ON p.ProductId = s.ProductId
    AND p.SumQty <> s.SumQty

      

+2


source


Select sum(qty) as Qty,ProductId from StockOutward where Orderid='38' group by ProductId 
except
Select sum(qty) as Qty,ProductId from ProductOutward where Orderid='38' group by ProductId

      



+1


source







All Articles