select 高一(1)=sum(case when 班级='高一(1)' then 1 else 0 end), 高一(2)=sum(case when 班级='高一(2)' then 1 else 0 end) from tb group by 班级
select '高一(1)'=sum(case when 班级='高一(1)' then 1 else 0 end), '高一(2)'=sum(case when 班级='高一(2)' then 1 else 0 end) from 表
--测试: create table tb(姓名 varchar(5), 年龄 int , 班级 varchar(10)) insert into tb select '张三',11,'高一(2)'union all select '李四',12,'高一(1)' union all select '王五',12,'高一(2)' union all select '赵六',12,'高一(2)' select '高一(1)'=sum(case when 班级='高一(1)' then 1 else 0 end), '高一(2)'=sum(case when 班级='高一(2)' then 1 else 0 end) from tbdrop table tb--结果: 高一(1) 高一(2) ----------- ----------- 1 3
create table tab(姓名 char(8),年龄 int,班级 char(10)) insert into tab select '张三', 11 , '高一(2)' union all select '李四', 12 , '高一(1)' union all select '王五', 12 , '高一(2)' union all select '赵六', 12 , '高一(2)'select sum(case 班级 when '高一(1)' then 1 else 0 end) as '高一(1)', sum(case 班级 when '高一(2)' then 1 else 0 end) as '高一(2)' from tab 高一(1) 高一(2) ----------- ----------- 1 3(所影响的行数为 1 行)
班级='高一(1)' case 班级 when '高一(1)'我的意思是,在我统计之前不知道有‘高一(1)’或者‘高一(2)’要自己去发现以后再统计出来。
declare @sql varchar(8000) set @sql='' select @sql=@sql+',sum(case 班级 when '''+cast(班级 as varchar(10))+''' then 1 else 0 end) as ['+cast(班级 as varchar(10))+']' from tb group by 班级 order by 班级 ascexec('select 班级 '+@sql+' from tb group by 班级')--结果: 班级 高一(1) 高一(2) ---------- ----------- ----------- 高一(1) 1 0 高一(2) 0 3
create table tb(姓名 varchar(5), 年龄 int , 班级 varchar(10)) insert into tb select '张三',11,'高一(2)'union all select '李四',12,'高一(1)' union all select '王五',12,'高一(2)' union all select '赵六',12,'高一(2)' declare @sql varchar(8000) set @sql='' select @sql=@sql+',sum(case 班级 when '''+cast(班级 as varchar(10))+''' then 1 else 0 end) as ['+cast(班级 as varchar(10))+']' from tb group by 班级 order by 班级 asc --去掉第一个逗号 set @sql=substring(@sql,2,datalength(@sql)) exec('select '+@sql+' from tb')--结果: 高一(1) 高一(2) ----------- ----------- 1 3
或者: declare @sql varchar(8000) set @sql='' select @sql=case when @sql='' then @sql+'sum(case 班级 when '''+班级+''' then 1 else 0 end) as ['+班级+']' else @sql+',sum(case 班级 when '''+班级+''' then 1 else 0 end) as ['+班级+']' end from tb group by 班级 order by 班级 asc exec('select '+@sql+' from tb')
高一(2)=sum(case when 班级='高一(2)' then 1 else 0 end)
from tb group by 班级
'高一(2)'=sum(case when 班级='高一(2)' then 1 else 0 end)
from 表
create table tb(姓名 varchar(5), 年龄 int , 班级 varchar(10))
insert into tb
select '张三',11,'高一(2)'union all
select '李四',12,'高一(1)' union all
select '王五',12,'高一(2)' union all
select '赵六',12,'高一(2)' select '高一(1)'=sum(case when 班级='高一(1)' then 1 else 0 end),
'高一(2)'=sum(case when 班级='高一(2)' then 1 else 0 end)
from tbdrop table tb--结果:
高一(1) 高一(2)
----------- -----------
1 3
insert into tab select
'张三', 11 , '高一(2)' union all select
'李四', 12 , '高一(1)' union all select
'王五', 12 , '高一(2)' union all select
'赵六', 12 , '高一(2)'select sum(case 班级 when '高一(1)' then 1 else 0 end) as '高一(1)',
sum(case 班级 when '高一(2)' then 1 else 0 end) as '高一(2)'
from tab 高一(1) 高一(2)
----------- -----------
1 3(所影响的行数为 1 行)
case 班级 when '高一(1)'我的意思是,在我统计之前不知道有‘高一(1)’或者‘高一(2)’要自己去发现以后再统计出来。
set @sql=''
select @sql=@sql+',sum(case 班级 when '''+cast(班级 as varchar(10))+''' then 1 else 0 end) as ['+cast(班级 as varchar(10))+']'
from tb
group by 班级 order by 班级 ascexec('select 班级 '+@sql+' from tb group by 班级')--结果:
班级 高一(1) 高一(2)
---------- ----------- -----------
高一(1) 1 0
高一(2) 0 3
insert into tb
select '张三',11,'高一(2)'union all
select '李四',12,'高一(1)' union all
select '王五',12,'高一(2)' union all
select '赵六',12,'高一(2)'
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',sum(case 班级 when '''+cast(班级 as varchar(10))+''' then 1 else 0 end) as ['+cast(班级 as varchar(10))+']'
from tb
group by 班级 order by 班级 asc
--去掉第一个逗号
set @sql=substring(@sql,2,datalength(@sql))
exec('select '+@sql+' from tb')--结果:
高一(1) 高一(2)
----------- -----------
1 3
declare @sql varchar(8000)
set @sql=''
select @sql=case when @sql='' then @sql+'sum(case 班级 when '''+班级+''' then 1 else 0 end) as ['+班级+']'
else @sql+',sum(case 班级 when '''+班级+''' then 1 else 0 end) as ['+班级+']' end
from tb
group by 班级 order by 班级 asc
exec('select '+@sql+' from tb')