asp:查询每个学生的各科成绩,总分,平均分的sql语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp:查询每个学生的各科成绩,总分,平均分的sql语句相关的知识,希望对你有一定的参考价值。
只有一个score表,字段是:xjh name zuohao xueke score
L001 张三 01 语文 65
L002 李四 02 数学 85
L001 张三 01 数学 75
L002 李四 02 语文 55
我想要的结果是: xjh name zuohao yuwen shuxue zongfen pjf
L001 张三 01 65 75 140 70
L002 李四 02 55 85 140 70
下面是我的sql语句:
dim rs,sql,sql2,zt,ck,nf,xq,cx,bj,bjmc,info
nf="2016"
xq="春季学期"
cx="期中考试"
bjmc="小学2012级3班"
set rs=server.CreateObject("Adodb.Recordset")
sql="select zuohao,name,xjh,"
sql=sql&"sum(case when xueke='语文' then score else 0 end) as yuwen,"
sql=sql&"sum(case when xueke='数学' then score else 0 end) as shuxue,"
sql=sql&"sum(case when xueke='英语' then score else 0 end) as yingyu,"
sql=sql&"sum(case when xueke='综合' then score else 0 end) as zhonghe,"
sql=sql&"sum(score) as zongfen,"
sql=sql&"sum(score)*1.0/4 as pjf"
sql=sql&" from score where nianfen='"&nf&"' and xueqi='"&xq&"' and cixu='"&cx&"' and bjmc='"&bjmc&"' group by zuohao,name,xjh order by desc zongfen"
rs.open sql,conn,1,1
提示错误:错误 '80004005' ,/StudentScore.asp,行 92 (注:92行就是:rs.open sql,conn,1,1)
怎么办啊?谢谢大虾
nf="2016"
xq="春季学期"
cx="期中考试"
bjmc="小学2012级3班"
set rs=server.CreateObject("Adodb.Recordset")
sql="select zuohao,name,xjh,"
sql=sql&"sum(case when xueke='语文' then score else 0 end) as yuwen,"
sql=sql&"sum(case when xueke='数学' then score else 0 end) as shuxue,"
sql=sql&"sum(case when xueke='英语' then score else 0 end) as yingyu,"
sql=sql&"sum(case when xueke='综合' then score else 0 end) as zhonghe,"
sql=sql&"sum(score) as zongfen,"
sql=sql&"sum(score)*1.0/4 as pjf"
sql=sql&" from score where nianfen='"&nf&"' and xueqi='"&xq&"' and cixu='"&cx&"' and bjmc='"&bjmc&"' group by zuohao,name,xjh order by zongfen desc "
rs.open sql,conn,1,1追问
错在哪个地方哦?没看出来,先谢过了
追答order by desc zongfen 写错了
-->order by zongfen desc
我改过来了,还是提示同样的错误代码
查询每个学生的各科成绩sql语句
数据库中有三个表 Student , Course, Grade 分别表示 学生, 课程,成绩
表的结构如下: Student( studentId, name, sex) 分别表示: 学号,姓名, 性别
Course ( cid , cname) 分别表示: 课程号,课程名
Grade ( gid, studentId , cid, score) 分别表示: 成绩编号, 学号, 课程号, 成绩
现在要查询每个学生的各科成绩和平均分
查询结果打印出的样式如下:
序号 姓名 性别 语文 数学 英语 哲学 平均成绩
1 王五 男 80 70 80 90 80
2 李明 女 90 70 70 80 77.5
列名 语文, 数学, 英语, 哲学 是 Course 中的课程名, 查询出的样式一定要与上面的一样,(假设 Course 表中 cname 只有四个 语文, 数学, 英语, 哲学)
在线等,如果答出来了,本人一定重赏
1、查询每个学生的各科成绩sql语句:
select a.studentid,a.name,a.sex,v1.score as '语文',v2.score as '数学', v3.score as '英语',v4.score
as ‘哲学’, (v1.score+v2.score+v3.score+v4.score)/4 as ‘平均成绩’ from Stuednt a
left join
(select studentid,score from grade where cid=(select cid from course where cname='语文'))as v1
on a.studentid=v1.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='数学'))as v2
on a.studentid=v2.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='英语'))as v3
on a.studentid=v3.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='哲学'))as v4
on a.studentid=v4.studentid
order by a.studentid
2、sql数据库介绍:
(1)SQL是Structured Query Language(结构化查询语言)的缩写。SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言。在使用它时,只需要发出"做什么"的命令,"怎么做"是不用使用者考虑的。
(2)SQL功能强大、简单易学、使用方便,已经成为了数据库操作的基础,并且现在几乎所有的数据库均支持SQL。
(3)SQL数据库的数据体系结构基本上是三级结构,但使用术语与传统关系模型术语不同。
(4)在SQL中,关系模式(模式)称为"基本表"(base table);存储模式(内模式)称为"存储文件"(stored file);子模式(外模式)称为"视图"(view);元组称为"行"(row);属性称为"列"(column)。
参考技术A mysql> select * from student;+-----------+------+------+
| studentId | name | sex |
+-----------+------+------+
| 1 | 王五 | 男 |
| 2 | 李明 | 女 |
+-----------+------+------+
2 rows in set (0.00 sec)
mysql> select * from course;
+-----+-------+
| cid | cname |
+-----+-------+
| 1 | 语文 |
| 2 | 数学 |
| 3 | 英语 |
| 4 | 哲学 |
+-----+-------+
4 rows in set (0.00 sec)
mysql> select * from grade;
+-----+-----------+------+-------+
| gid | studentId | cid | score |
+-----+-----------+------+-------+
| 1 | 1 | 1 | 80 |
| 2 | 1 | 2 | 70 |
| 3 | 1 | 3 | 80 |
| 4 | 1 | 4 | 90 |
| 5 | 2 | 1 | 90 |
| 6 | 2 | 2 | 70 |
| 7 | 2 | 3 | 70 |
| 8 | 2 | 4 | 80 |
+-----+-----------+------+-------+
8 rows in set (0.00 sec)
mysql> select
-> grade.studentId as '序号',
-> name as '姓名',
-> sex as '性别',
-> sum(if(cname = '语文', score, 0)) as '语文',
-> sum(if(cname = '数学', score, 0)) as '数学',
-> sum(if(cname = '英语', score, 0)) as '英语',
-> sum(if(cname = '哲学', score, 0)) as '哲学',
-> avg(score) as '平均成绩'
-> from
-> grade
-> left join
-> student
-> on grade.studentId = student.studentId
-> left join
-> course
-> on grade.cid = course.cid
-> group by grade.studentId;
+------+------+------+------+------+------+------+----------+
| 序号 | 姓名 | 性别 | 语文 | 数学 | 英语 | 哲学 | 平均成绩 |
+------+------+------+------+------+------+------+----------+
| 1 | 王五 | 男 | 80 | 70 | 80 | 90 | 80.0000 |
| 2 | 李明 | 女 | 90 | 70 | 70 | 80 | 77.5000 |
+------+------+------+------+------+------+------+----------+
2 rows in set (0.00 sec)
mysql 的这样试试,其它数据库就不会了。
嗯,mysql 也是关系数据库,语法和 sqlserver 差不多,多谢谢你啊
追答:D
参考技术B selectrow_number() over(order by name) as 序号
t.name as 姓名,
t.sex as 性别,
max(case when t.cname='语文' then score end) as 语文,
max(case when t.cname='数学' then score end) as 数学,
max(case when t.cname='英语' then score end) as 英语,
max(case when t.cname='哲学' then score end) as 哲学
from
(select a.name,a.sex,b.cname,c.score
from student a,course b,grade c
where a.studentid=c.studentid
and b.cid=c.cid) as t
group by t.name,t.sex 参考技术C select a.studentId,a.name,a.sex,c.cid,b.cname,c.score
into TableA
from Student a, Course b, Grade c
where a.studentId=c.studentId and c.cid=b.cid
select a.studentId,a.name,a.sex,
sum(case cname when "语文" then score else 0 end) as 语文,
sum(case cname when "数学" then score else 0 end) as 数学,
sum(case cname when "英语" then score else 0 end) as 英语,
sum(case cname when "哲学" then score else 0 end) as 哲学,
sum(score)*1.0/4 as "平均成绩"
from TableA
group by name追问
这个语句你执行了没,怎么执行不对?
本回答被提问者采纳 参考技术D select a.studentid,a.name,a.sex,v1.score as '语文',v2.score as '数学', v3.score as '英语',v4.scoreas ‘哲学’, (v1.score+v2.score+v3.score+v4.score)/4 as ‘平均成绩’ from Stuednt a
left join
(select studentid,score from grade where cid=(select cid from course where cname='语文'))as v1
on a.studentid=v1.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='数学'))as v2
on a.studentid=v2.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='英语'))as v3
on a.studentid=v3.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='哲学'))as v4
on a.studentid=v4.studentid
order by a.studentid追问
这个确实能执行,执行的结果与样式的一模一样,不过sql语句太复杂了,能简单些吗?
追答需要简单的结果的过程有时候是很复杂的,其实这个语句还算简单的,只不过每个科目的成绩都是个视图,然后连接起来。
有些查询嵌套多层的子查询,那才复杂呢。
以上是关于asp:查询每个学生的各科成绩,总分,平均分的sql语句的主要内容,如果未能解决你的问题,请参考以下文章
java真实面试_翰竺科技有限公司_牛客网上的选择题sql面试题_查询各科总分最高的前5个学生_查询各科考试的平均分
用java二维数组完成两个同学三科成绩,计算每位同学的总分, 各科的平均分