日期:2014-05-17 浏览次数:20524 次
--> 测试数据:tableA
if object_id('tableA ') is not null drop table tableA
go
create table tableA ([Pid] int,[Pdate] datetime,Pname varchar(10),PSum int)
insert tableA
select 1,'2013-10-1','张三',9 union all
select 2,'2013-10-1','李四',19 union all
select 3,'2013-10-1','王二麻',8 union all
select 4,'2013-10-1','张三',38 union all
select 5,'2013-10-1','李四',11 union all
select 6,'2013-10-1','王二麻',2 union all
select 7,'2013-10-1','李四',15
select pname,[PSum小于10]=sum(case when psum <10 then 1 else 0 end ),
[PSum10-20]=sum(case when psum >=10 and psum <20 then 1 else 0 end ),
[PSum大于20]=sum(case when psum >20 then 1 else 0 end )
from tableA
group by pname
if object_id('tableA ') is not null drop table tableA
go
create table tableA ([Pid] int,[Pdate] datetime,Pname varchar(10),PSum int)
insert&nb