日期:2014-05-18 浏览次数:20518 次
--> 测试数据:[point]
if object_id('[point]') is not null drop table [point]
create table [point]([POINT] int,[TYPE] int)
insert [point]
select 3,1 union all
select 4,2 union all
select 2,1 union all
select 4,1 union all
select 1,2 union all
select 3,1
select case when [type]=2 then -[POINT] else [POINT] end as [POINT],
[TYPE] from [point]
/*
POINT TYPE
3 1
-4 2
2 1
4 1
-1 2
3 1
*/
--求和:
select [TYPE],sum(case when [type]=2 then -[POINT] else [POINT] end) as [POINT]
from [point] group by [TYPE]
/*
TYPE POINT
1 12
2 -5
*/
------解决方案--------------------
--求和:
select sum(case when [type]=2 then -[POINT] else [POINT] end) as [POINT]
from [point]
/*
POINT
7
*/