SQL显示库存更新

时间:2017-07-03 01:44:49

标签: sql sql-server-ce

我有一个表格,显示库存的所有更新:

╔══════════════╦════════╦══════════╦═══════╗
║   ItemName   ║  Type  ║ Quantity ║ Date  ║
╠══════════════╬════════╬══════════╬═══════╣
║ BottledWater ║ Add    ║       50 ║ 07/03 ║
║ BottledWater ║ Deduct ║       20 ║ 07/03 ║
║ Chips        ║ Add    ║       30 ║ 07/02 ║
║ BottledWater ║ Deduct ║       10 ║ 07/02 ║
║ Chips        ║ Deduct ║       20 ║ 07/01 ║
╚══════════════╩════════╩══════════╩═══════╝

我想写一个查询并返回一个表格,该表格会显示已添加和扣除的股票的总数,该表格如下所示:

╔══════════════╦═══════╦══════════╗
║   ItemName   ║ Added ║ Deducted ║
╠══════════════╬═══════╬══════════╣
║ BottledWater ║    50 ║       30 ║
║ Chips        ║    30 ║       20 ║
╚══════════════╩═══════╩══════════╝

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

只需使用条件聚合:

0000000 b848 beef dead babe cafe               
000000a

答案 1 :(得分:0)

语法稍微关闭,但这是伪代码:

select ItemName, sum(Type==add) as added, sum(Type==deduct) as deducted, from inventory group by ItemName