表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
表product list
:
id product inward outward stock
1 vs29 20 12 8
我需要asp.net的sql语句。
答案 0 :(得分:0)
我不确切地知道你想要什么,但我想你想做这样的事情:
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链接:http://sqlfiddle.com/#!6/65f06e/5