-4

先生..你能告诉我这个PLZ的查询吗

表格1

--------------------------------
stock_name|temple|quantity|type
--------------------------------
rice      |shiva |  32    |purchase
rice      |shiva |  10    |issue
wheat     |ganesh|  50    | purchase
wheat     |ganesh|  45    | issue   
ghee      |ganesh|  50    | purchase
ghee      |ganesh|   5    | issue   

表 2 中的预期输出

--------------------------------
stock_name|temple|quantity|
--------------------------------
rice      |shiva |  22    |
wheat     |ganesh|  5     | 
ghee      |ganesh|  45    |
4

1 回答 1

3

这里的答案与您发布的所有其他问题一样

INSERT INTO secondTable (stock_name,temple,quantity)
SELECT  stock_name,
        temple,
        SUM(CASE WHEN type = 'purchase' 
                     THEN quantity 
                     ELSE quantity * -1 
                 END) quantity
FROM    firstTable
GROUP   BY stock_name, temple
于 2013-05-21T13:00:03.333 回答