日期:2014-05-16 浏览次数:20477 次
create table xe
(id varchar(10),name varchar(10),num int,isout int)
insert into xe
select 'A','aa',20,1 union all
select 'A','aa',10,1 union all
select 'A','aa',10,0 union all
select 'B','bb',30,1 union all
select 'C','cc',10,0 union all
select 'C','cc',10,0
select id,name,
sum(case isout when 1 then num else 0 end) 'out',
sum(case isout when 1 then 0 else num end) 'int'
from xe
group by id,name
/*
id name out int
---------- ---------- ----------- -----------
A aa 30 10
B bb 30 0
C cc 0 20
(3 row(s) affected)
*/
select id,name,
sum(case when isout = 1 then Num else 0 end) as 'out',
sum(case when isout = 0 then Num else 0 end) as 'in'
from 表名
group by id,nam