日期:2014-05-18 浏览次数:20650 次
select
sum(case when flag='2' then cishu else 0 end)-sum(case when flag in ('5','128') then cishu else 0 end)
as [手工充值+补贴〉月消费的值]
from tb
------解决方案--------------------
--> 测试数据:[tbl]
if object_id('[tbl]') is not null drop table [tbl]
create table [tbl]([ID] varchar(3),[flag] int,[cishu] int)
insert [tbl]
select '003',128,3 union all
select '003',5,6 union all
select '007',5,6 union all
select '007',2,7 union all
select '003',2,9
select [ID] from(
select [ID],case [flag] when 128 then [cishu] when 5 then [cishu]
else -[cishu] end as [cishu] from tbl)a group by ID having SUM(cishu)>=0