Imran Bashir Imran Bashir - 4 months ago 6
SQL Question

return true if the column which have the same ProductID

i have table name stock in which i have 5 column name id

productId
stockIn
stockOut
And
VoucherType
..

i want to check the if
productId=1
and
voucherType=1
that count all the column in
stockIn
with the same
ProductId
and Sum All the value in
stockIn
then also count in
stockout
with same product id and sum if the
stockIn
sum is greater then
stockOut
that return true...

id ' productid ' stockin 'stockout 'VoucherType

-----------------------------------------
1 | 1 | 5 | 0 | 1
2 | 1 | 5 | 0 | 1
3 | 1 | 5 | 0 | 1
4 | 2 | 5 | 0 | 1
5 | 2 | 0 | 10 | 2
6 | 1 | 0 | 2 | 2
7 | 2 | 0 | 3 | 2

Answer
    SELECT productId
    , voucherType
    , COUNT(stockIn) as Count_stockIn
    , SUM(stockIn) as Sum_stockIn
    , COUNT(stockOut) as Count_stockOut
    , CASE WHEN SUM(stockIn) > SUM(stockOut) THEN 'True' ELSE 'False' END AS IsInGreater
From Table
Group By productId, voucherType
Having productId = voucherType And IsInGreater = 'True'

This is kind of what you want? (your question is seriously unclear)