select No,
[count]=sum([count]),
oldcount=sum(case oldnew when 1 then [count] end),
newcount=sum([count])-sum(case oldnew when 1 then [count] end)
from 表 group by NO
select
No,
count = sum([count]),
oldcount = sum(case when oldnew = 0 then [count] else 0 end),
newcount = sum(case when oldnew = 0 then [count] else 0 end)
from
t
group by
No
order by
No
修改
select No,
count=SUM(count),
oldcount=SUM(Case oldnew when 0 then [count] End),
newcount=SUM(Case oldnew when 1 then [count] End)
from 表 Group by NO
select no sum(count) count ,
sum(case when oldnew=0 then count else 0 end) oldcount
sum(case when oldnew=1 then count else 0 end) newcount
from table
group by no
select No,
[count]=sum([count]),
oldcount=sum(case oldnew when 0 then [count] end),
newcount=sum([count])-sum(case oldnew when 0 then [count] end)
from 表 group by NO
--建立测试环境
Create Table 表(NO varchar(10),count integer,oldnew integer)
--插入数据
insert into 表
select '001','1','0' union
select '001','2','0' union
select '001','2','1' union
select '002','1','1' union
select '002','2','1' union
select '002','2','0'
select * from 表
--测试语句
select [no],sum([count])[count],sum(case when oldnew=1 then [count] else 0 end)[newcount],
sum(case when oldnew=0 then [count] else 0 end)[oldcount]
from 表
group by [no]
select No,
[count]=sum([count]),
oldcount=sum(case oldnew when 1 then [count] end),
newcount=sum([count])-sum(case oldnew when 1 then [count] end)
from 表 group by NO
--建立测试环境
Create Table 表(NO varchar(10),count integer,oldnew integer)
--插入数据
insert into 表
select '001','1','0' union
select '001','2','0' union
select '001','2','1' union
select '002','1','1' union
select '002','2','1' union
select '002','2','0'
select * from 表
--测试语句
select [no],sum([count])[count],sum(case when oldnew=1 then [count] else 0 end)[oldcount],
sum(case when oldnew=0 then [count] else 0 end)[newcount]
from 表
group by [no]