34,588
社区成员
发帖
与我相关
我的任务
分享
select product,sum(case when BS='B' then Quantity else -Quantity end) as Quantity
from TB group by product
select Product ,
sum(case when BS = 'B' then Quantity else -Quantity end) as Quantity
from tb group by Product
select Product,sum(case BS when 'S' then Quantity else -Quantity end) as sl
from 表 group by product
select Product ,sum(case when BS = 'B' then Quantity else -Quantity end)
from tb
group by Product
select Product, sum((case when BS='B' then -1 else 1 end) *Quantity)
from tb
group by Product
select Product, sum((case when BS='B' then 1 else -1 end) *Quantity)
from tb
group by Product
create table tb(Product varchar(10),BS varchar(10), Quantity int)
insert into tb values('P1', 'B', 100)
insert into tb values('P1', 'B', 200)
insert into tb values('P1', 'S', 500)
insert into tb values('p2', 'B', 400)
insert into tb values('p2', 'S', 900)
insert into tb values('P2', 'S', 100)
go
select Product ,Quantity =
sum(case when BS = 'B' then -Quantity else Quantity end)
from tb
group by Product
/*
Product Quantity
---------- -----------
P1 200
p2 600
(所影响的行数为 2 行)
*/
select Product ,Quantity =
sum(case when BS = 'B' then -Quantity when bs = 's' then Quantity end)
from tb
group by Product
/*
Product Quantity
---------- -----------
P1 200
p2 600
(所影响的行数为 2 行)
*/
drop table tb