找出高考流水表中缺失的考试记录

关于高考流水表由来请看 https://www.cnblogs.com/xiandedanteng/p/12310596.html

高考流水表是这样一种结构:

学号 科目id 分数
231 1 99
34 2 128
8799 3 130
... ... ...
9912 5 105

在现实中,阅卷组将考生考卷批阅提交后就会形成流水表。

但由于学生未提交,遗失,火灾等原因,不是每个学生都能找到其五门功课的成绩,这些不足五条的记录给我们的统计和分析带来一定的麻烦,我们有必要将先其找出来。

下面的语句就能知道每个学生有几门功课的成绩:

select stuid,count(score) as sbjcount from gk_score group by stuid

但这样数据太多,真正缺失成绩只是极小一部分,下面SQL能精准定位他们:

select t1.stuid from
(
 select stuid,count(score) as sbjcount from gk_score group by stuid
) t1
where t1.sbjcount<5
SQL> select t1.stuid from
  2  (
  3   select stuid,count(score) as sbjcount from gk_score group by stuid
  4  ) t1
  5  where t1.sbjcount<5;

     STUID
----------
     70292
     26090
     60886

这下知道只有三名考生成绩不足五门了。

再来看看他们是哪些记录:

select * from gk_score where stuid in (
    select t1.stuid from
    (
     select stuid,count(score) as sbjcount from gk_score group by stuid
    ) t1
    where t1.sbjcount<5
)
order by stuid,subjectid
SQL> select * from gk_score where stuid in (
  2  select t1.stuid from
  3  (
  4   select stuid,count(score) as sbjcount from gk_score group by stuid
  5  ) t1
  6  where t1.sbjcount<5
  7  )
  8  order by stuid,subjectid;

        ID  SUBJECTID      STUID      SCORE
---------- ---------- ---------- ----------
    100001          2      26090         41
    200001          3      26090        110
    300001          4      26090         89
    400001          5      26090         97
    100011          2      60886         12
    200011          3      60886         16
    300011          4      60886         21
    400011          5      60886        129
    101111          2      70292        127
    201111          3      70292         84
    301111          4      70292          2

已选择11行。

这下能知道,26090和60886的考生缺失了科目1也就是语文成绩,70292的考生缺失了科目1和科目5的也就是语文额化学成绩。

找到缺失记录后,编写insert语句将其补上即可。

--2020年2月16日--

posted @ 2020-02-16 12:03  逆火狂飙  阅读(146)  评论(0)    收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东