22,209
社区成员
发帖
与我相关
我的任务
分享
DECLARE @Max INT,@Min INT,@Str NVARCHAR(3000)
select @MAX= max(三门), @MIN= MIN(三门) from zongchenjibiao WHERE 语文缺='0' and 数学缺='0' and 英语缺='0'
SELECT @Max=@Max/10*10,@Min=@Min/10*10,@str=N' Case '
WHILE @Min<=@Max
BEGIN
SELECT @str=@str+N' WHEN 三门 BETWEEN '+RTRIM(@MIN)+' AND '+rtrim(@MIN+9)+' THEN '''+RTRIM(@MIN)+'-'+rtrim(@MIN+9)+'''',@MIN=@MIN+10
END
EXEC ( 'select [分数段]='+@Str+' else ''不在范围'' end,'+
'sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''01'' then 1 else 0 end) as [高二01],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''02'' then 1 else 0 end) as [高二02],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''03'' then 1 else 0 end) as [高二03],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''04'' then 1 else 0 end) as [高二04],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''05'' then 1 else 0 end) as [高二05],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''06'' then 1 else 0 end) as [高二06],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''07'' then 1 else 0 end) as [高二07],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''08'' then 1 else 0 end) as [高二08],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''09'' then 1 else 0 end) as [高二09],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''10'' then 1 else 0 end) as [高二10],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''11'' then 1 else 0 end) as [高二11],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''12'' then 1 else 0 end) as [高二12],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''13'' then 1 else 0 end) as [高二13],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''14'' then 1 else 0 end) as [高二14],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''15'' then 1 else 0 end) as [高二15],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''16'' then 1 else 0 end) as [高二16],
sum(case when zongchenjibiao.年级= N''高二'' and zongchenjibiao.班级=''17'' then 1 else 0 end) as [高二17]
---into linshis
from dangqianbanji,zongchenjibiao
where dangqianbanji.班级=zongchenjibiao.班级 and 语文缺=0 and 数学缺=0 and 英语缺=0
group by '+@str+' else ''不在范围'' end')
declare @sql varchar(8000)
---select @sql= '高一['+min(班级)+']' from zongchenjibiao WHERE 语文缺='0' and 数学缺='0' and 英语缺='0'
---select @sql
select @sql =isnull(@sql + ',' , '') + 班级全称 from zongchenjibiao group by 班级全称
select @sql
exec ('select *
---into linshih
from (select 班级全称,三门 from zongchenjibiao) a pivot (max(三门) for 班级全称 in (' + @sql + ')) b')