sql语句

螃蟹大虾 2008-03-18 11:19:22
一张表 如有10000条,其中有一个分数字段a,一个是学校字段,我要以学校为组每100条累加成一记录.大于60分为合格求出合格率
例100条中有 60条大于60分 合格率为60%
1校 2校 3校
(1~100) 60% 80% 55%
(100~200) 70% ... ..
......
各位大侠 指点迷津

...全文
204 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiangHongTao 2008-03-19
  • 打赏
  • 举报
回复
12L 的不行吗?
螃蟹大虾 2008-03-19
  • 打赏
  • 举报
回复
可不可以详细点啊...高手...
螃蟹大虾 2008-03-18
  • 打赏
  • 举报
回复
自己顶顶
螃蟹大虾 2008-03-18
  • 打赏
  • 举报
回复
在线等~~~~~各位大侠 指点迷津
dawugui 2008-03-18
  • 打赏
  • 举报
回复
麻烦,下午再来.
螃蟹大虾 2008-03-18
  • 打赏
  • 举报
回复
表结构
id classID score
1 1 60
2 1 70
3 2 50
4 2 80
5 1 55
6 3 90
....

合格率 以classID= 1为例(classID (1-100) 中大于60的条数/100 ====60%
(classID (100-200) 中大于60的条数/100 ====80%

新表
id class1(avg) class2(avg) class3(avg) ...
1(1-100) 60% 50% 60%
2(100-200) 80% 70% 66%
....
wzy_love_sly 2008-03-18
  • 打赏
  • 举报
回复
select 0/100,99/100,101/100,199/100,200/100,299/100

0 0 1 1 2 2


加个自增列,按列分组?
dawugui 2008-03-18
  • 打赏
  • 举报
回复
张表 如有10000条,其中有一个分数字段a,一个是学校字段,我要以学校为组每100条累加成一记录.大于60分为合格求出合格率
例100条中有 60条大于60分 合格率为60%
1校 2校 3校
(1~100) 60% 80% 55%
(100~200) 70% ... ..
......
各位大侠 指点迷津

-----------------------------------

给个具体的表结构和测试数据来.
pt1314917 2008-03-18
  • 打赏
  • 举报
回复
以学校为组,每100条统计一次,这个麻烦。。
liangCK 2008-03-18
  • 打赏
  • 举报
回复
/*
标题:普通行列转换(version 2.0)
作者:爱新觉罗.毓华
时间:2008-03-09
地点:广东深圳
说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。

问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果):
姓名 语文 数学 物理
---- ---- ---- ----
李四 74 84 94
张三 74 83 93
-------------------
*/

create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go

--SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)
select 姓名 as 姓名 ,
max(case 课程 when '语文' then 分数 else 0 end) 语文,
max(case 课程 when '数学' then 分数 else 0 end) 数学,
max(case 课程 when '物理' then 分数 else 0 end) 物理
from tb
group by 姓名

--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql)

--SQL SERVER 2005 静态SQL。
select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b

--SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + 课程 from tb group by 课程
exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')

---------------------------------

/*
问题:在上述结果的基础上加平均分,总分,得到如下结果:
姓名 语文 数学 物理 平均分 总分
---- ---- ---- ---- ------ ----
李四 74 84 94 84.00 252
张三 74 83 93 83.33 250
*/

--SQL SERVER 2000 静态SQL。
select 姓名 姓名,
max(case 课程 when '语文' then 分数 else 0 end) 语文,
max(case 课程 when '数学' then 分数 else 0 end) 数学,
max(case 课程 when '物理' then 分数 else 0 end) 物理,
cast(avg(分数*1.0) as decimal(18,2)) 平均分,
sum(分数) 总分
from tb
group by 姓名

--SQL SERVER 2000 动态SQL。
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名'
exec(@sql)

--SQL SERVER 2005 静态SQL。
select m.* , n.平均分 , n.总分 from
(select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b) m,
(select 姓名 , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名) n
where m.姓名 = n.姓名

--SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + 课程 from tb group by 课程
exec ('select m.* , n.平均分 , n.总分 from
(select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b) m ,
(select 姓名 , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名) n
where m.姓名 = n.姓名')

drop table tb

------------------
------------------

/*
问题:如果上述两表互相换一下:即表结构和数据为:
姓名 语文 数学 物理
张三 74  83  93
李四 74  84  94
想变成(得到如下结果):
姓名 课程 分数
---- ---- ----
李四 语文 74
李四 数学 84
李四 物理 94
张三 语文 74
张三 数学 83
张三 物理 93
--------------
*/

create table tb(姓名 varchar(10) , 语文 int , 数学 int , 物理 int)
insert into tb values('张三',74,83,93)
insert into tb values('李四',74,84,94)
go

--SQL SERVER 2000 静态SQL。
select * from
(
select 姓名 , 课程 = '语文' , 分数 = 语文 from tb
union all
select 姓名 , 课程 = '数学' , 分数 = 数学 from tb
union all
select 姓名 , 课程 = '物理' , 分数 = 物理 from tb
) t
order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 end

--SQL SERVER 2000 动态SQL。
--调用系统表动态生态。
declare @sql varchar(8000)
select @sql = isnull(@sql + ' union all ' , '' ) + ' select 姓名 , [课程] = ' + quotename(Name , '''') + ' , [分数] = ' + quotename(Name) + ' from tb'
from syscolumns
where name! = N'姓名' and ID = object_id('tb') --表名tb,不包含列名为姓名的其它列
order by colid asc
exec(@sql + ' order by 姓名 ')

--SQL SERVER 2005 动态SQL。
select 姓名 , 课程 , 分数 from tb unpivot (分数 for 课程 in([语文] , [数学] , [物理])) t

--SQL SERVER 2005 动态SQL,同SQL SERVER 2000 动态SQL。

--------------------
/*
问题:在上述的结果上加个平均分,总分,得到如下结果:
姓名 课程 分数
---- ------ ------
李四 语文 74.00
李四 数学 84.00
李四 物理 94.00
李四 平均分 84.00
李四 总分 252.00
张三 语文 74.00
张三 数学 83.00
张三 物理 93.00
张三 平均分 83.33
张三 总分 250.00
------------------
*/

select * from
(
select 姓名 as 姓名 , 课程 = '语文' , 分数 = 语文 from tb
union all
select 姓名 as 姓名 , 课程 = '数学' , 分数 = 数学 from tb
union all
select 姓名 as 姓名 , 课程 = '物理' , 分数 = 物理 from tb
union all
select 姓名 as 姓名 , 课程 = '平均分' , 分数 = cast((语文 + 数学 + 物理)*1.0/3 as decimal(18,2)) from tb
union all
select 姓名 as 姓名 , 课程 = '总分' , 分数 = 语文 + 数学 + 物理 from tb
) t
order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 when '平均分' then 4 when '总分' then 5 end

drop table tb
liangCK 2008-03-18
  • 打赏
  • 举报
回复
行转列?
xthe 2008-03-18
  • 打赏
  • 举报
回复
单独对学校进行统计,然后join
螃蟹大虾 2008-03-18
  • 打赏
  • 举报
回复
再顶一下...完美答案呢
螃蟹大虾 2008-03-18
  • 打赏
  • 举报
回复
rtrim(sum(jg)*100/count(*))+''%'' jgl
---
一百条统计一次
螃蟹大虾 2008-03-18
  • 打赏
  • 举报
回复
是啊
dawugui 2008-03-18
  • 打赏
  • 举报
回复
假设字段为(id school name score),其中ID不能重复,同校+同名+同分的可能性也很小。

select jl ,
sum(case school when '一校' then score_qk else 0 end) / 100 as '一校',
sum(case school when '二校' then score_qk else 0 end) / 100 as '二校',
sum(case school when '三校' then score_qk else 0 end) / 100 as '三校'
from
(
select school,
jl = cast((px - 1)/100*100 + 1 as varchar) + '~' + cast((px - 1)/100 + 100 as varchar) ,
score_qk = case when score >= 60 then 100 else 0 end
from
(
select t.* , px = (select count(1) from tb where (school = t.shcool and score < t.score) or (school = t.shcool and name = t.name and score < t.score)) + 1 from tb t
) m
) n
group by jl
jhs1982419 2008-03-18
  • 打赏
  • 举报
回复
你意思是说10000条记录可能来自不同的学校吗 ?
JiangHongTao 2008-03-18
  • 打赏
  • 举报
回复
--构建测试数据
set nocount on
create table tb(xx varchar(10) ,score int)
declare @i int,@j int
select @i = 1
while @i <=5
begin
set @j= 1
while @j <=1000
begin
insert tb select '学校'+rtrim(@i),@j%100
set @j = @j +1
end
set @i = @i+1
end
go
declare @s varchar(8000)
select identity(int,1,1) id,xx,case when score >=60 then 1 else 0 end jg into # from tb order by xx
select @s = 'select xh 序号'
select @s = @s+',max(case when xx = '''+xx+''' then jgl else '''' end) as '+xx from # group by xx order by xx
select @s = @s +' from (
select ''[''+rtrim((a.id-b.id)/100*100)+''-''+rtrim((a.id-b.id)/100*100+99)+'']'' xh
,a.xx,rtrim(sum(jg)*100/count(*))+''%'' jgl from # a join (select xx,min(id) id from # group by xx) b
on a.xx = b.xx group by ''[''+rtrim((a.id-b.id)/100*100)+''-''+rtrim((a.id-b.id)/100*100+99)+'']'',a.xx) c
group by xh'
exec(@s)
/*
序号 学校1 学校2 学校3 学校4 学校5
--------------------------- ------------- ------------- ------------- ------------- -------------
[0-99] 40% 40% 40% 40% 40%
[100-199] 40% 40% 40% 40% 40%
[200-299] 40% 40% 40% 40% 40%
[300-399] 40% 40% 40% 40% 40%
[400-499] 40% 40% 40% 40% 40%
[500-599] 40% 40% 40% 40% 40%
[600-699] 40% 40% 40% 40% 40%
[700-799] 40% 40% 40% 40% 40%
[800-899] 40% 40% 40% 40% 40%
[900-999] 40% 40% 40% 40% 40%
*/
drop table #
go
drop table tb
pt1314917 2008-03-18
  • 打赏
  • 举报
回复
依此类推。
pt1314917 2008-03-18
  • 打赏
  • 举报
回复

create table tb(id int identity(1,1),classID int,score int)
insert into tb select 1,62
insert into tb select 1,74
insert into tb select 2,50
insert into tb select 2,90
insert into tb select 1,25
insert into tb select 3,62


declare @sql varchar(8000)
set @sql='select ''1(1-100)''[id]'
select @sql=@sql+',[class'+ ltrim(classid) +
'(avg)]=ltrim(cast(sum(case when classid='''+ ltrim(classid) +''' and score>60 then 1 else 0 end)*100.0/
sum(case when classid='''+ ltrim(classid) +''' then 1 else 0 end) as numeric(5,2)))+''%'''
from (select distinct classid from tb)a
set @sql=@sql+' from tb a where id in(select top 100 id from tb where classid=a.classid)'
exec(@sql)

--上面是算出1-100的。还要算100-200等等。。最后将结果union all就行了。。。


22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧