高手请进,很复杂的纵表转横表问题 不嫌麻烦的话,每个区间算一次,union all 20次 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 -------------------------------------- Author: happyflystone -- Version:V1.001 -- Date:2009-06-12 -------------------------------------- Test Data: taIf object_id('ta') is not null Drop table taGoCreate table ta(学校 nvarchar(3),考号 int,成绩 int)GoInsert into ta select '学校A',1022,99 union all select '学校A',1023,98 union all select '学校B',1024,55 union all select '学校C',1025,88 Go--StartSelect cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ', (select count(1) from ta) '全区人数', *into Tmpfrom ta aright join (select number from master..spt_values where type = 'P' and number <20) bon a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+'['+学校+'百分比]= cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*1.000/全区人数 as numeric(12,2))'from (select distinct 学校 from ta) aexec('select 分数区间,全区人数,全区百分比 = cast(sum(case when 考号 is null then 0 else 1 end)*1.00/全区人数 as numeric(12,2)),'+@s+' from tmpgroup by 分数区间,全区人数order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')drop table tmp--Result:/*分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比 ----- ----------- --------- ----------- ---------- ----------- -------------- ----------- ------- 0-5 4 .00 0 .00 0 .00 0 .005-10 4 .00 0 .00 0 .00 0 .0010-15 4 .00 0 .00 0 .00 0 .0015-20 4 .00 0 .00 0 .00 0 .0020-25 4 .00 0 .00 0 .00 0 .0025-30 4 .00 0 .00 0 .00 0 .0030-35 4 .00 0 .00 0 .00 0 .0035-40 4 .00 0 .00 0 .00 0 .0040-45 4 .00 0 .00 0 .00 0 .0045-50 4 .00 0 .00 0 .00 0 .0050-55 4 .25 0 .00 1 .25 0 .0055-60 4 .00 0 .00 0 .00 0 .0060-65 4 .00 0 .00 0 .00 0 .0065-70 4 .00 0 .00 0 .00 0 .0070-75 4 .00 0 .00 0 .00 0 .0075-80 4 .00 0 .00 0 .00 0 .0080-85 4 .00 0 .00 0 .00 0 .0085-90 4 .25 0 .00 0 .00 1 .2590-95 4 .00 0 .00 0 .00 0 .0095-100 4 .50 2 .50 0 .00 0 .00*/--End -------------------------------------- Author: happyflystone -- Version:V1.001 -- Date:2009-06-12 23:25:44-------------------------------------- Test Data: taIf object_id('ta') is not null Drop table taGoCreate table ta(学校 nvarchar(3),考号 int,成绩 int)GoInsert into ta select '学校A',1022,99 union all select '学校a',1023,45 union all select '学校B',1024,55 union all select '学校C',1025,88 Go--StartSelect cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ', (select count(1) from ta) '全区人数', *into Tmpfrom ta aright join (select number from master..spt_values where type = 'P' and number <20) bon a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+'['+学校+'百分比]= cast(cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*100.000/全区人数 as numeric(12,2)) as varchar)+''%'''from (select distinct 学校 from ta) aexec('select 分数区间,全区人数=sum(case when 考号 is null then 0 else 1 end) ,全区百分比 = cast(cast(sum(case when 考号 is null then 0 else 1 end)*100.00/全区人数 as numeric(12,2)) as varchar)+''%'' ,'+@s+' from tmpgroup by 分数区间,全区人数order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')drop table tmp--Result:/*分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比 ----- ----------- -------- ----------- ----------- ----------- ------ ----------- ---------- 0-5 0 0.00% 0 0.00% 0 0.00% 0 0.00%5-10 0 0.00% 0 0.00% 0 0.00% 0 0.00%10-15 0 0.00% 0 0.00% 0 0.00% 0 0.00%15-20 0 0.00% 0 0.00% 0 0.00% 0 0.00%20-25 0 0.00% 0 0.00% 0 0.00% 0 0.00%25-30 0 0.00% 0 0.00% 0 0.00% 0 0.00%30-35 0 0.00% 0 0.00% 0 0.00% 0 0.00%35-40 0 0.00% 0 0.00% 0 0.00% 0 0.00%40-45 1 25.00% 1 25.00% 0 0.00% 0 0.00%45-50 0 0.00% 0 0.00% 0 0.00% 0 0.00%50-55 1 25.00% 0 0.00% 1 25.00% 0 0.00%55-60 0 0.00% 0 0.00% 0 0.00% 0 0.00%60-65 0 0.00% 0 0.00% 0 0.00% 0 0.00%65-70 0 0.00% 0 0.00% 0 0.00% 0 0.00%70-75 0 0.00% 0 0.00% 0 0.00% 0 0.00%75-80 0 0.00% 0 0.00% 0 0.00% 0 0.00%80-85 0 0.00% 0 0.00% 0 0.00% 0 0.00%85-90 1 25.00% 0 0.00% 0 0.00% 1 25.00%90-95 0 0.00% 0 0.00% 0 0.00% 0 0.00%95-100 1 25.00% 1 25.00% 0 0.00% 0 0.00%*/--End ---盗版的 试下新的签名 感觉真的不错 谢谢石头哥 ------------------------------------------------------------------ Author :fredrickhu(小F 向高手学习)-- Date :2009-06-12 23:53:42------------------------------------------------------------------> 测试数据:@tbdeclare @tb table([学校] varchar(5),[考号] int,[成绩] int)insert @tbselect '学校A',1022,99 union allselect '学校A',1023,45 union allselect '学校B',1024,55 union allselect '学校C',1025,88Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ', (select count(1) from @tb) '全区人数', *into Tmpfrom @tb aright join (select number from master..spt_values where type = 'P' and number <20) bon a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+'['+学校+'百分比]= cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*1.000/全区人数 as numeric(12,2))'from (select distinct 学校 from @tb) aexec('select 分数区间,全区人数,全区百分比 = cast(sum(case when 考号 is null then 0 else 1 end)*1.00/全区人数 as numeric(12,2)),'+@s+' from tmpgroup by 分数区间,全区人数order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')drop table tmp--Result:/*分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比 ----- ----------- --------- ----------- ---------- ----------- -------------- ----------- ------- 0-5 4 .00 0 .00 0 .00 0 .005-10 4 .00 0 .00 0 .00 0 .0010-15 4 .00 0 .00 0 .00 0 .0015-20 4 .00 0 .00 0 .00 0 .0020-25 4 .00 0 .00 0 .00 0 .0025-30 4 .00 0 .00 0 .00 0 .0030-35 4 .00 0 .00 0 .00 0 .0035-40 4 .00 0 .00 0 .00 0 .0040-45 4 .00 0 .00 0 .00 0 .0045-50 4 .00 0 .00 0 .00 0 .0050-55 4 .25 0 .00 1 .25 0 .0055-60 4 .00 0 .00 0 .00 0 .0060-65 4 .00 0 .00 0 .00 0 .0065-70 4 .00 0 .00 0 .00 0 .0070-75 4 .00 0 .00 0 .00 0 .0075-80 4 .00 0 .00 0 .00 0 .0080-85 4 .00 0 .00 0 .00 0 .0085-90 4 .25 0 .00 0 .00 1 .2590-95 4 .00 0 .00 0 .00 0 .0095-100 4 .50 2 .50 0 .00 0 .00*/--End ------------------------------------------------------------------ Author :fredrickhu(小F 向高手学习)-- Date :2009-06-12 23:53:42------------------------------------------------------------------> 测试数据:@tbdeclare @tb table([学校] varchar(5),[考号] int,[成绩] int)insert @tbselect '学校A',1022,99 union allselect '学校A',1023,45 union allselect '学校B',1024,55 union allselect '学校C',1025,88Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ', (select count(1) from @tb) '全区人数', *into Tmpfrom @tb aright join (select number from master..spt_values where type = 'P' and number <20) bon a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+'['+学校+'百分比]= cast(cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*100.000/全区人数 as numeric(12,2)) as varchar)+''%'''from (select distinct 学校 from @tb) aexec('select 分数区间,全区人数=sum(case when 考号 is null then 0 else 1 end) ,全区百分比 = cast(cast(sum(case when 考号 is null then 0 else 1 end)*100.00/全区人数 as numeric(12,2)) as varchar)+''%'' ,'+@s+' from tmpgroup by 分数区间,全区人数order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')drop table tmp/*分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比 ----- ----------- -------- ----------- ----------- ----------- ------ ----------- ---------- 0-5 0 0.00% 0 0.00% 0 0.00% 0 0.00%5-10 0 0.00% 0 0.00% 0 0.00% 0 0.00%10-15 0 0.00% 0 0.00% 0 0.00% 0 0.00%15-20 0 0.00% 0 0.00% 0 0.00% 0 0.00%20-25 0 0.00% 0 0.00% 0 0.00% 0 0.00%25-30 0 0.00% 0 0.00% 0 0.00% 0 0.00%30-35 0 0.00% 0 0.00% 0 0.00% 0 0.00%35-40 0 0.00% 0 0.00% 0 0.00% 0 0.00%40-45 1 25.00% 1 25.00% 0 0.00% 0 0.00%45-50 0 0.00% 0 0.00% 0 0.00% 0 0.00%50-55 1 25.00% 0 0.00% 1 25.00% 0 0.00%55-60 0 0.00% 0 0.00% 0 0.00% 0 0.00%60-65 0 0.00% 0 0.00% 0 0.00% 0 0.00%65-70 0 0.00% 0 0.00% 0 0.00% 0 0.00%70-75 0 0.00% 0 0.00% 0 0.00% 0 0.00%75-80 0 0.00% 0 0.00% 0 0.00% 0 0.00%80-85 0 0.00% 0 0.00% 0 0.00% 0 0.00%85-90 1 25.00% 0 0.00% 0 0.00% 1 25.00%90-95 0 0.00% 0 0.00% 0 0.00% 0 0.00%95-100 1 25.00% 1 25.00% 0 0.00% 0 0.00%*/ -------------------------------------- Author: happyflystone -- Version:V1.001 -- Date:2009-06-12 -------------------------------------- Test Data: taIf object_id('ta') is not null Drop table taGoCreate table ta(学校 nvarchar(3),考号 int,成绩 int)GoInsert into ta select '学校A',1022,99 union all select '学校A',1023,98 union all select '学校B',1024,55 union all select '学校C',1025,88 Go--StartSelect cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ', (select count(1) from ta) '全区人数', *into Tmpfrom ta aright join (select number from master..spt_values where type = 'P' and number <20) bon a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+'['+学校+'百分比]= cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*1.000/全区人数 as numeric(12,2))'from (select distinct 学校 from ta) aexec('select 分数区间,全区人数,全区百分比 = cast(sum(case when 考号 is null then 0 else 1 end)*1.00/全区人数 as numeric(12,2)),'+@s+' from tmpgroup by 分数区间,全区人数order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')drop table tmp--Result:/*分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比 ----- ----------- --------- ----------- ---------- ----------- -------------- ----------- ------- 0-5 4 .00 0 .00 0 .00 0 .005-10 4 .00 0 .00 0 .00 0 .0010-15 4 .00 0 .00 0 .00 0 .0015-20 4 .00 0 .00 0 .00 0 .0020-25 4 .00 0 .00 0 .00 0 .0025-30 4 .00 0 .00 0 .00 0 .0030-35 4 .00 0 .00 0 .00 0 .0035-40 4 .00 0 .00 0 .00 0 .0040-45 4 .00 0 .00 0 .00 0 .0045-50 4 .00 0 .00 0 .00 0 .0050-55 4 .25 0 .00 1 .25 0 .0055-60 4 .00 0 .00 0 .00 0 .0060-65 4 .00 0 .00 0 .00 0 .0065-70 4 .00 0 .00 0 .00 0 .0070-75 4 .00 0 .00 0 .00 0 .0075-80 4 .00 0 .00 0 .00 0 .0080-85 4 .00 0 .00 0 .00 0 .0085-90 4 .25 0 .00 0 .00 1 .2590-95 4 .00 0 .00 0 .00 0 .0095-100 4 .50 2 .50 0 .00 0 .00*/--End 引用一下。 --SQL 2005可以用declare @s varchar(max) 1. SQL 2005 有 nvarchar(max)2. SQL 2000 要用多个子串。例如 declare @s1 varchar(8000), @s2 varchar(8000), @s3 varchar(8000) --分别赋值 EXEC(@s1+@s2+@s3) 表关联查询问题 【【简单sql抽取数据效率问题???】】 ERWin4.1缺少license,求解决方式! 求助smalldatetime字段中查询的问题 跨数据库建立主外键关系 查询重复数据问题。 用SQL怎样把getdate得到的结果转换成yyyy-mm-dd格式 帮我把这条sql语句简化一下 关于脚本的问题! 为什么SELECT CURRENT_USER不用加括号(),而SELECT USER_NAME()要加括号才能运行呢? 求出最近三天的日期 like查询
-- Author: happyflystone
-- Version:V1.001
-- Date:2009-06-12
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(学校 nvarchar(3),考号 int,成绩 int)
Go
Insert into ta
select '学校A',1022,99 union all
select '学校A',1023,98 union all
select '学校B',1024,55 union all
select '学校C',1025,88
Go
--Start
Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ',
(select count(1) from ta) '全区人数',
*
into Tmp
from ta a
right join (select number from master..spt_values where type = 'P' and number <20) b
on a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)
select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+
'['+学校+'百分比]= cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*1.000/全区人数 as numeric(12,2))'
from (select distinct 学校 from ta) a
exec('
select 分数区间,全区人数,全区百分比 = cast(sum(case when 考号 is null then 0 else 1 end)*1.00/全区人数 as numeric(12,2)),'+@s+'
from tmp
group by 分数区间,全区人数
order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')
drop table tmp
--Result:
/*
分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比
----- ----------- --------- ----------- ---------- ----------- -------------- ----------- -------
0-5 4 .00 0 .00 0 .00 0 .00
5-10 4 .00 0 .00 0 .00 0 .00
10-15 4 .00 0 .00 0 .00 0 .00
15-20 4 .00 0 .00 0 .00 0 .00
20-25 4 .00 0 .00 0 .00 0 .00
25-30 4 .00 0 .00 0 .00 0 .00
30-35 4 .00 0 .00 0 .00 0 .00
35-40 4 .00 0 .00 0 .00 0 .00
40-45 4 .00 0 .00 0 .00 0 .00
45-50 4 .00 0 .00 0 .00 0 .00
50-55 4 .25 0 .00 1 .25 0 .00
55-60 4 .00 0 .00 0 .00 0 .00
60-65 4 .00 0 .00 0 .00 0 .00
65-70 4 .00 0 .00 0 .00 0 .00
70-75 4 .00 0 .00 0 .00 0 .00
75-80 4 .00 0 .00 0 .00 0 .00
80-85 4 .00 0 .00 0 .00 0 .00
85-90 4 .25 0 .00 0 .00 1 .25
90-95 4 .00 0 .00 0 .00 0 .00
95-100 4 .50 2 .50 0 .00 0 .00*/
--End
-- Author: happyflystone
-- Version:V1.001
-- Date:2009-06-12 23:25:44
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(学校 nvarchar(3),考号 int,成绩 int)
Go
Insert into ta
select '学校A',1022,99 union all
select '学校a',1023,45 union all
select '学校B',1024,55 union all
select '学校C',1025,88
Go
--Start
Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ',
(select count(1) from ta) '全区人数',
*
into Tmp
from ta a
right join (select number from master..spt_values where type = 'P' and number <20) b
on a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)
select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+
'['+学校+'百分比]= cast(cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*100.000/全区人数 as numeric(12,2)) as varchar)+''%'''
from (select distinct 学校 from ta) a
exec('
select 分数区间,全区人数=sum(case when 考号 is null then 0 else 1 end) ,全区百分比 = cast(cast(sum(case when 考号 is null then 0 else 1 end)*100.00/全区人数 as numeric(12,2)) as varchar)+''%'' ,'+@s+'
from tmp
group by 分数区间,全区人数
order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')
drop table tmp
--Result:
/*
分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比
----- ----------- -------- ----------- ----------- ----------- ------ ----------- ----------
0-5 0 0.00% 0 0.00% 0 0.00% 0 0.00%
5-10 0 0.00% 0 0.00% 0 0.00% 0 0.00%
10-15 0 0.00% 0 0.00% 0 0.00% 0 0.00%
15-20 0 0.00% 0 0.00% 0 0.00% 0 0.00%
20-25 0 0.00% 0 0.00% 0 0.00% 0 0.00%
25-30 0 0.00% 0 0.00% 0 0.00% 0 0.00%
30-35 0 0.00% 0 0.00% 0 0.00% 0 0.00%
35-40 0 0.00% 0 0.00% 0 0.00% 0 0.00%
40-45 1 25.00% 1 25.00% 0 0.00% 0 0.00%
45-50 0 0.00% 0 0.00% 0 0.00% 0 0.00%
50-55 1 25.00% 0 0.00% 1 25.00% 0 0.00%
55-60 0 0.00% 0 0.00% 0 0.00% 0 0.00%
60-65 0 0.00% 0 0.00% 0 0.00% 0 0.00%
65-70 0 0.00% 0 0.00% 0 0.00% 0 0.00%
70-75 0 0.00% 0 0.00% 0 0.00% 0 0.00%
75-80 0 0.00% 0 0.00% 0 0.00% 0 0.00%
80-85 0 0.00% 0 0.00% 0 0.00% 0 0.00%
85-90 1 25.00% 0 0.00% 0 0.00% 1 25.00%
90-95 0 0.00% 0 0.00% 0 0.00% 0 0.00%
95-100 1 25.00% 1 25.00% 0 0.00% 0 0.00%*/
--End
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-06-12 23:53:42
----------------------------------------------------------------
--> 测试数据:@tb
declare @tb table([学校] varchar(5),[考号] int,[成绩] int)
insert @tb
select '学校A',1022,99 union all
select '学校A',1023,45 union all
select '学校B',1024,55 union all
select '学校C',1025,88
Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ',
(select count(1) from @tb) '全区人数',
*
into Tmp
from @tb a
right join (select number from master..spt_values where type = 'P' and number <20) b
on a.成绩 between b.number * 5 + 1 and (b.number+1)*5
declare @s varchar(8000)
select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+
'['+学校+'百分比]= cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*1.000/全区人数 as numeric(12,2))'
from (select distinct 学校 from @tb) a
exec('
select 分数区间,全区人数,全区百分比 = cast(sum(case when 考号 is null then 0 else 1 end)*1.00/全区人数 as numeric(12,2)),'+@s+'
from tmp
group by 分数区间,全区人数
order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')
drop table tmp
--Result:
/*
分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比
----- ----------- --------- ----------- ---------- ----------- -------------- ----------- -------
0-5 4 .00 0 .00 0 .00 0 .00
5-10 4 .00 0 .00 0 .00 0 .00
10-15 4 .00 0 .00 0 .00 0 .00
15-20 4 .00 0 .00 0 .00 0 .00
20-25 4 .00 0 .00 0 .00 0 .00
25-30 4 .00 0 .00 0 .00 0 .00
30-35 4 .00 0 .00 0 .00 0 .00
35-40 4 .00 0 .00 0 .00 0 .00
40-45 4 .00 0 .00 0 .00 0 .00
45-50 4 .00 0 .00 0 .00 0 .00
50-55 4 .25 0 .00 1 .25 0 .00
55-60 4 .00 0 .00 0 .00 0 .00
60-65 4 .00 0 .00 0 .00 0 .00
65-70 4 .00 0 .00 0 .00 0 .00
70-75 4 .00 0 .00 0 .00 0 .00
75-80 4 .00 0 .00 0 .00 0 .00
80-85 4 .00 0 .00 0 .00 0 .00
85-90 4 .25 0 .00 0 .00 1 .25
90-95 4 .00 0 .00 0 .00 0 .00
95-100 4 .50 2 .50 0 .00 0 .00*/
--End
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-06-12 23:53:42
----------------------------------------------------------------
--> 测试数据:@tb
declare @tb table([学校] varchar(5),[考号] int,[成绩] int)
insert @tb
select '学校A',1022,99 union all
select '学校A',1023,45 union all
select '学校B',1024,55 union all
select '学校C',1025,88Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ',
(select count(1) from @tb) '全区人数',
*
into Tmp
from @tb a
right join (select number from master..spt_values where type = 'P' and number <20) b
on a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)
select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+
'['+学校+'百分比]= cast(cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*100.000/全区人数 as numeric(12,2)) as varchar)+''%'''
from (select distinct 学校 from @tb) a
exec('
select 分数区间,全区人数=sum(case when 考号 is null then 0 else 1 end) ,全区百分比 = cast(cast(sum(case when 考号 is null then 0 else 1 end)*100.00/全区人数 as numeric(12,2)) as varchar)+''%'' ,'+@s+'
from tmp
group by 分数区间,全区人数
order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')
drop table tmp
/*
分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比
----- ----------- -------- ----------- ----------- ----------- ------ ----------- ----------
0-5 0 0.00% 0 0.00% 0 0.00% 0 0.00%
5-10 0 0.00% 0 0.00% 0 0.00% 0 0.00%
10-15 0 0.00% 0 0.00% 0 0.00% 0 0.00%
15-20 0 0.00% 0 0.00% 0 0.00% 0 0.00%
20-25 0 0.00% 0 0.00% 0 0.00% 0 0.00%
25-30 0 0.00% 0 0.00% 0 0.00% 0 0.00%
30-35 0 0.00% 0 0.00% 0 0.00% 0 0.00%
35-40 0 0.00% 0 0.00% 0 0.00% 0 0.00%
40-45 1 25.00% 1 25.00% 0 0.00% 0 0.00%
45-50 0 0.00% 0 0.00% 0 0.00% 0 0.00%
50-55 1 25.00% 0 0.00% 1 25.00% 0 0.00%
55-60 0 0.00% 0 0.00% 0 0.00% 0 0.00%
60-65 0 0.00% 0 0.00% 0 0.00% 0 0.00%
65-70 0 0.00% 0 0.00% 0 0.00% 0 0.00%
70-75 0 0.00% 0 0.00% 0 0.00% 0 0.00%
75-80 0 0.00% 0 0.00% 0 0.00% 0 0.00%
80-85 0 0.00% 0 0.00% 0 0.00% 0 0.00%
85-90 1 25.00% 0 0.00% 0 0.00% 1 25.00%
90-95 0 0.00% 0 0.00% 0 0.00% 0 0.00%
95-100 1 25.00% 1 25.00% 0 0.00% 0 0.00%*/
-- Author: happyflystone
-- Version:V1.001
-- Date:2009-06-12
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(学校 nvarchar(3),考号 int,成绩 int)
Go
Insert into ta
select '学校A',1022,99 union all
select '学校A',1023,98 union all
select '学校B',1024,55 union all
select '学校C',1025,88
Go
--Start
Select cast(ltrim(b.number * 5 )+'-'+ltrim((b.number+1)*5) as varchar) as '分数区间 ',
(select count(1) from ta) '全区人数',
*
into Tmp
from ta a
right join (select number from master..spt_values where type = 'P' and number <20) b
on a.成绩 between b.number * 5 + 1 and (b.number+1)*5declare @s varchar(8000)
select @s = isnull(@s+',','') + '['+学校+'人数]= sum( case when 学校 = '''+学校+''' then 1 else 0 end),'+
'['+学校+'百分比]= cast(sum( case when 学校 = '''+学校+''' then 1 else 0 end)*1.000/全区人数 as numeric(12,2))'
from (select distinct 学校 from ta) a
exec('
select 分数区间,全区人数,全区百分比 = cast(sum(case when 考号 is null then 0 else 1 end)*1.00/全区人数 as numeric(12,2)),'+@s+'
from tmp
group by 分数区间,全区人数
order by cast(left(分数区间,charindex(''-'',分数区间) -1) as int)')
drop table tmp
--Result:
/*
分数区间 全区人数 全区百分比 学校A人数 学校A百分比 学校B人数 学校B百分比 学校C人数 学校C百分比
----- ----------- --------- ----------- ---------- ----------- -------------- ----------- -------
0-5 4 .00 0 .00 0 .00 0 .00
5-10 4 .00 0 .00 0 .00 0 .00
10-15 4 .00 0 .00 0 .00 0 .00
15-20 4 .00 0 .00 0 .00 0 .00
20-25 4 .00 0 .00 0 .00 0 .00
25-30 4 .00 0 .00 0 .00 0 .00
30-35 4 .00 0 .00 0 .00 0 .00
35-40 4 .00 0 .00 0 .00 0 .00
40-45 4 .00 0 .00 0 .00 0 .00
45-50 4 .00 0 .00 0 .00 0 .00
50-55 4 .25 0 .00 1 .25 0 .00
55-60 4 .00 0 .00 0 .00 0 .00
60-65 4 .00 0 .00 0 .00 0 .00
65-70 4 .00 0 .00 0 .00 0 .00
70-75 4 .00 0 .00 0 .00 0 .00
75-80 4 .00 0 .00 0 .00 0 .00
80-85 4 .00 0 .00 0 .00 0 .00
85-90 4 .25 0 .00 0 .00 1 .25
90-95 4 .00 0 .00 0 .00 0 .00
95-100 4 .50 2 .50 0 .00 0 .00*/
--End 引用一下。
declare @s varchar(max)
2. SQL 2000 要用多个子串。例如
declare @s1 varchar(8000), @s2 varchar(8000), @s3 varchar(8000)
--分别赋值
EXEC(@s1+@s2+@s3)