Asp.net 根据两列求一个sql列的和,并根据条件求另一个表的和
表Asp.net 根据两列求一个sql列的和,并根据条件求另一个表的和,asp.net,sql,Asp.net,Sql,表teststock: type date time product qty in 26-Apr 6.5 vs29 10 out 26-Apr 5.4 vs29 2 in 26-Apr 8.9 vs29 5 out 25-Apr 7.2 vs29 10 in 27-Apr 5.2 vs29 5 表产品列表: id product inward outward stock 1 vs29 20 12 8 我需要a
teststock
:
type date time product qty
in 26-Apr 6.5 vs29 10
out 26-Apr 5.4 vs29 2
in 26-Apr 8.9 vs29 5
out 25-Apr 7.2 vs29 10
in 27-Apr 5.2 vs29 5
表产品列表
:
id product inward outward stock
1 vs29 20 12 8
我需要asp.net的sql语句
我不知道你到底想要什么,但我认为你想要做这样的事情:
select a.product , sum(a.inward) inward ,sum(a.outward) outward , sum(a.inward) - sum(a.outward) stock from (
select product , case when type = 'in' then qty else 0 end inward ,
case when type = 'out' then qty else 0 end outward
from test_stock) a
group by a.product
这是关于sql查询的sqlfiddle链接:所以您想用该信息填充
产品列表
,还是该信息已经存在?像往常一样,您尝试了什么?对我来说似乎是个糟糕的设计。产品列表应该只有库存水平,当添加/删除测试库存时,库存水平应该作为插入的一部分进行调整。