如果具有相同ProductID的列,则返回true

时间:2016-07-30 10:11:01

标签: sql sql-server tsql

我有表名称库存,其中我有5列名称inputs productId stockInstockOut ..

我想检查VoucherTypeproductId=1是否计算voucherType=1中具有相同stockIn的所有列,并ProductId中的所有值如果stockIn总和大于stockout并返回true,则还会在stockIn中计入相同的产品ID和总和...

stockOut

1 个答案:

答案 0 :(得分:0)

    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'

这是你想要的吗? (你的问题很严重)