您的位置:程序门 -> db2 -> 数据库开发



向各位数据库高手求教!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


向各位数据库高手求教!
发表于:2007-07-21 23:13:05 楼主
数据库中scorenew表中有如下数据:记录了一个学校比较乱的学生成绩记录,每个学生对应不同的课程,一个课程又至少有两个老师,在2006年里考试不及格的同学(result='f'),可在2007年补考,若补考过了则该学生该门课程是认为合格的(如2002101),也可以在2006考试虽然是及格,但由于分数刚好60,也允许同学补考,但补考成绩不及格则被认为该生课程是不合格的(如2002102也就是对同一个学生id,课程成绩以该门课程考试时间最大的那个为准).
学号                     课程名               老师                             考试结果         考试日期
studentid           classname         classteacher             result             testdate
2002100               数学                   b2老师                         p                       2006-7-3
2002100               外语                   c1老师                         p                       2006-7-5
2002100               语文                   a1老师                         p                       2006-7-1
2002101               数学                   b2老师                         p                       2006-7-3
2002101               语文                   a1老师                         f                       2006-7-1
2002101               语文                   a1老师                         p                       2007-7-1
2002102               数学                   b1老师                         p                       2006-7-3
2002102               数学                   b1老师                         f                       2007-7-3
2002102               外语                   c1老师                         p                       2006-7-5
2002102               语文                   a1老师                         f                       2006-7-1
2002102               语文                   a1老师                         f                       2007-7-1
2002103               数学                   b1老师                         p                       2006-7-3
2002103               外语                   c2老师                         p                       2006-7-5
2002103               语文                   a2老师                         p                       2007-7-1
2002104               数学                   b2老师                         p                       2006-7-3
2002104               外语                   c1老师                         f                       2006-7-5
2002104               外语                   c1老师                         p                       2007-7-5
2002104               语文                   a2老师                         p                       2006-7-1
.现在求下面几个sql语句:
1.求所有科目都及格的人数.(已经解决)
2.求参加某门科目的人数和该门科目及格的人数.
3.求2006年度所有科目都及格的人数,即一次性考试通过的人数.
4.求某个时间范围内某个科目及格的人数.
呵呵如果你是数据库高手,不凡挑战下自己,这也许对你来说是个小case.
发表于:2007-07-21 23:31:261楼 得分:0
先把第一问的答案奉献给大家!
1.先求出总人数
select   count(distinct   studentid)   as   总人数   from   scorenew
结果=5
2.再求出考试科目最终有不及格的个数。
select   studentid,   result
from   (select   studentid,   classname,   result,   testdate
                from   scorenew   t
                where   not   exists
                                    (select   1
                                  from   scorenew
                                  where   studentid   =   t   .studentid   and  
                                              classname   =   t   .classname   and   testdate   >   t   .testdate))   a
group   by   studentid,   result
having   (result   =   'f ')
结果显示
2002102       f
改成如下语句
select   count(distinct   studentid)   as   不及格人数
from   (select   studentid,   result
                from   (select   studentid,   classname,   result,   testdate
                                from   scorenew   t
                                where   not   exists
                                                    (select   1
                                                  from   scorenew
                                                  where   studentid   =   t   .studentid   and  
                                                              classname   =   t   .classname   and   testdate   >   t   .testdate))  
                            a
                group   by   studentid,   result
                having   (result   =   'f '))  
having   (result   =   'f ')
显示:
不及格人数
1
所以合格人数=5-1=4
发表于:2007-07-22 00:07:472楼 得分:0
第二问统计某门课程的参加人数和不及格人数
以科目数学为例:
第一步:先求出参加该门课程的总人数
select   count(distinct   studentid)   as   该科目总人数
from   scorenew
where   (classname   =   '数学 ')
结果显示为5
第二步:
select   studentid,   result
from   (select   studentid,   classname,   result,   testdate
                from   scorenew   t
                where   not   exists
                                    (select   1
                                  from   scorenew
                                  where   studentid   =   t   .studentid   and  
                                              classname   =   t   .classname   and   testdate   >   t   .testdate)   and  
                            classname   =   '数学 ')   a
group   by   studentid,   result
having   (result   =   'f ')
显示结果
2002102     f
改写成统计该科目不及格人数
select   count(distinct   studentid)   as   该科目不及格人数
from   (select   studentid,   classname,   result,   testdate
                from   scorenew   t
                where   not   exists
                                    (select   1
                                  from   scorenew
                                  where   studentid   =   t   .studentid   and  
                                              classname   =   t   .classname   and   testdate   >   t   .testdate)   and  
                            classname   =   '数学 ')   a
group   by   studentid,   result
having   (result   =   'f ')
该科目不及格人数
1
发表于:2007-07-22 00:08:363楼 得分:0
先把第一问的答案奉献给大家!
1.先求出总人数
select   count(distinct   studentid)   as   总人数   from   scorenew
结果=5
2.再求出考试科目最终有不及格的个数。
select   studentid,   result
from   (select   studentid,   classname,   result,   testdate
                from   scorenew   t
                where   not   exists
                                    (select   1
                                  from   scorenew
                                  where   studentid   =   t   .studentid   and  
                                              classname   =   t   .classname   and   testdate   >   t   .testdate))   a
group   by   studentid,   result
having   (result   =   'f ')
结果显示
2002102       f
改成如下语句
select   count(distinct   studentid)   as   不及格人数
from   (select   studentid,   result
                from   (select   studentid,   classname,   result,   testdate
                                from   scorenew   t
                                where   not   exists
                                                    (select   1
                                                  from   scorenew
                                                  where   studentid   =   t   .studentid   and  
                                                              classname   =   t   .classname   and   testdate   >   t   .testdate))  
                            a
                group   by   studentid,   result
                having   (result   =   'f '))  
having   (result   =   'f ')
显示:
不及格人数
1
所以合格人数=5-1=4


快速检索

最新资讯
热门点击