用数据库的知识做
学号 姓名 英语 数学 物理 化学 VB
200235882 张一山 81 75 63 58 87
200235883 张二山 81 75 63 58 87
200235882 张撒山 81 75 63 58 87
200235893 李的斯 71 87 73 78 97
200236004 王永民 91 58 83 88 67
200236016 李鹏 87 79 93 98 77
200236088 胡萝卜 51 95 69 76 37
200236099 赵来财 61 77 77 87 89
200236113 钱广 78 56 57 76 81
200236124 孙悟空 88 68 89 66 67
200236132 周全 21 75 73 87 57
200236152 吴大庆 96 85 93 67 17
200236182 赵无财 61 77 77 87 89
200235883 张说山 81 75 63 58 87
200235883 张的山 81 75 63 58 87
200235883 张飞山 81 75 63 58 87
200235891 李斯 71 87 73 78 97
200236000 王永民 91 58 83 88 67
200236010 李鹏 87 79 93 98 77
200236088 胡萝卜 51 95 69 45 37
200236099 赵来财 61 77 77 87 89
200236115 钱广 78 56 57 76 81
200236125 孙悟空 88 68 89 66 67
200236134 周全 21 75 73 87 57
200236152 吴大庆 96 85 93 67 17
200236181 赵无财 61 77 77 87 89
统计出分数段范围人数
范围 英语 数学 物理 化学 VB
>=90 3 5 .......
80-90 10 12 ......
....
....
<60 .... .... ....
然后就是算出每个人的加权平均分,进行排名~~~
谢谢各位大侠了~~我菜鸟在线等
------解决方案--------------------
- SQL code
select '[>=90]'范围 , sum(case when 英语>=90 then 1 else 0 end)英语, sum(case when 数学>=90 then 1 else 0 end)数学, sum(case when 物理>=90 then 1 else 0 end)物理, sum(case when 化学>=90 then 1 else 0 end)化学, sum(case when VB>=90 then 1 else 0 end)VBfrom tbunion allselect '[80-90]'范围 , sum(case when 英语 between 80 and 90 then 1 else 0 end)英语, sum(case when 数学 between 80 and 90 then 1 else 0 end)数学, sum(case when 物理 between 80 and 90 then 1 else 0 end)物理, sum(case when 化学 between 80 and 90 then 1 else 0 end)化学, sum(case when VB between 80 and 90 then 1 else 0 end)VBfrom tb....
------解决方案--------------------
- SQL code
CREATE TABLE tb(ID int,Num int)INSERT tb SELECT 1,2UNION ALL ALL SELECT 6,2UNION ALL SELECT 7,1UNION ALL SELECT 8,5UNION ALL SELECT 9,1GO--查询的存储过程CREATE PROC p_Qry@group VARCHAR(1000)ASSET NOCOUNT ONIF @group LIKE '%[^0-9,]%'BEGIN RAISERROR(N'"%s" 中包含非数字数据',1,16,@group) RETURNEND--将字符串分拆为分组表DECLARE @t TABLE(ID int IDENTITY,Groups varchar(10),a int,b int)DECLARE @i int,@pid varchar(10)SELECT @i=CHARINDEX(',',@group+',') ,@pid=LEFT(@group,@i-1) ,@group=STUFF(@group,1,@i,'')+',' ,@i=CHARINDEX(',',@group)INSERT @t SELECT 'ID<[email protected],NULL,@pidWHILE @i>1BEGIN INSERT @t SELECT @pid+'<ID<='+LEFT(@group,@i-1),@pid,LEFT(@group,@i-1) SELECT @pid=LEFT(@group,@i-1) ,@group=STUFF(@group,1,@i,'') ,@i=CHARINDEX(',',@group)ENDINSERT @t SELECT 'ID>[email protected],@pid,NULL--根据分组表统计SELECT b.Groups,Num=ISNULL(SUM(a.Num),0)FROM tb a RIGHT JOIN @t b ON (a.ID<=b.b OR b.b IS NULL) AND(a.ID>b.a OR b.a IS NULL)GROUP BY b.ID,b.GroupsORDER BY b.IDGO--调用存储过程进行查询EXEC p_Qry '2,3,6'/*--测试结果Groups Num ---------- ----------- ID<=2 52<ID<=3 23<ID<=6 16ID>6 7--*/
------解决方案--------------------
- SQL code
-----------------------------------------------> Author : js_szy --> Target : ★★★ --> Date : 2009-12-16 12:11:24--> Version: SQL Server 2005--------------------------------------------- --> 测试数据: @tbdeclare @tb table (学号 int,姓名 varchar(6),英语 int,数学 int,物理 int,化学 int,VB int)insert into @tbselect 200235882,'张一山',81,75,63,58,87 union allselect 200235883,'张二山',81,75,63,58,87 union allselect 200235882,'张撒山',81,75,63,58,87 union allselect 200235893,'李的斯',71,87,73,78,97 union allselect 200236004,'王永民',91,58,83,88,67 union allselect 200236016,'李鹏',87,79,93,98,77 union allselect 200236088,'胡萝卜',51,95,69,76,37 union allselect 200236099,'赵来财',61,77,77,87,89 union allselect 200236113,'钱广',78,56,57,76,81 union allselect 200236124,'孙悟空',88,68,89,66,67 union allselect 200236132,'周全',21,75,73,87,57 union allselect 200236152,'吴大庆',96,85,93,67,17 union allselect 200236182,'赵无财',61,77,77,87,89 union allselect 200235883,'张说山',81,75,63,58,87 union allselect 200235883,'张的山',81,75,63,58,87 union allselect 200235883,'张飞山',81,75,63,58,87 union allselect 200235891,'李斯',71,87,73,78,97 union allselect 200236000,'王永民',91,58,83,88,67 union allselect 200236010,'李鹏',87,79,93,98,77 union allselect 200236088,'胡萝卜',51,95,69,45,37 union allselect 200236099,'赵来财',61,77,77,87,89 union allselect 200236115,'钱广',78,56,57,76,81 union allselect 200236125,'孙悟空',88,68,89,66,67 union allselect 200236134,'周全',21,75,73,87,57 union allselect 200236152,'吴大庆',96,85,93,67,17 union allselect 200236181,'赵无财',61,77,77,87,89select 范围=case when 英语>=90 then '[>=90]' when 英语>=80 and 英语<90 then '[80-90]' when 英语>=70 and 英语<80 then '[70-80]' when 英语>=60 and 英语<70 then '[60-70]' when 英语<60 then '[<60]' end ,英语=count(*)from @tbgroup by case when 英语>=90 then '[>=90]' when 英语>=80 and 英语<90 then '[80-90]' when 英语>=70 and 英语<80 then '[70-80]' when 英语>=60 and 英语<70 then '[60-70]' when 英语<60 then '[<60]' end--....其他的类似范围 英语------- -----------[<60] 4[>=90] 4[60-70] 4[70-80] 4[80-90] 10(5 行受影响)