我有商店产品进货表的详细资料。
当我用价格输入产品时,用储存库和数量输入产品。
通过获得具有存储库和数量的产品id来获得输出产品。
现在,我需要清点存储库中的产品清单。
示例:
Bii详细数据:
Type Product_ID Repository_id Qty
1 2 2 1
1 2 2 3
2 2 2 1
1 2 3 2
1 2 3 3
2 2 3 2
注:输入: 1:输入,2:输出
折叠数据:存储库2中产品2的总库存有:
Total Input - Total output = (1+3) - 1 = 3
我需要sql用于:-列表所有存储库都有产品:示例与产品2,有2个存储库2,以及3 -产品存储库的总库存:示例,与产品2和存储库3。总库存为3。
那么,如何使用SQL进行查询呢?
发布于 2017-03-26 00:12:07
在SQL中
当基于类型时,可以使用sum和group和case。
select
Product_ID
, Repository_id
, sum(case when type = 1 then Qty
when typ2 = 2 then -Qty
END) total
from my_table
group by
Product_ID
, Repository_id
https://stackoverflow.com/questions/43026445
复制