SQL查询,根据学生的表现自动选择和加“'成绩列'

时间:2017-11-18 10:39:25

标签: java sql

我需要真正的帮助......

我有一张学生成绩及其相关职位的表格 - 就像这样:

+---------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
|  Name   | Grade1 | G1_Pos | Grade2 | G2_Pos | Grade3 | G3_Pos | Grade4 | Grade5 | Agregate |
+---------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
|  james  |    4   | 3rd    |     6  | 4th    |    1   | 1st    |     3  |      2 |          |
|  Mavis  |    1   | 1st    |     1  | 1st    |    3   | 3rd    |     2  |      3 |          |
|  john   |    5   | 4th    |     3  | 2nd    |    4   | 4th    |     4  |      4 |          |
|  bendo  |    2   | 2nd    |     5  | 3rd    |    2   | 2nd    |     1  |      2 |          |
+---------+--------+--------+--------+--------+--------+--------+--------+--------+----------+

NB:Pos意思是位置..

我想总结每个学生的成绩列,并将他们的结果存储在聚合列中。但是,我想要总结五个等级列中的四个等级列,而不是总结所有等级列。 Grade4和Grade5列必须是要添加的四列的一部分。

为此,我只有这段代码

String sql = "update jhs1 set Agregate=(SELECT SUM(Grade4+Grade5) AS ST) WHERE ID = jhs1.ID";
pst = conn.prepareStatement(sql);
pst.execute();

对于其余三个成绩列,我想要一个代码,它将检查并挑选出学生表现良好的三个成绩列中最好的两个成绩列。例如。如果詹姆斯的结果是Grade1 = 4,Grade2 = 6,Grade3 = 1 ..代码必须只选择这三个等级中最好的两个等级 例如,Grade1 = 4,Grade3 = 1,并将结果添加到Grade4和Grade5。

所以在詹姆斯案例中,它将是4 + 1 + 3 + 2 = 10.

请问如何编写这样的SQL查询并得到如下表所示的结果:

+---------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
|  Name   | Grade1 | G1_Pos | Grade2 | G2_Pos | Grade3 | G3_Pos | Grade4 | Grade5 | Agregate |
+---------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
|  james  |    4   | 3rd    |     6  | 4th    |    1   | 1st    |     3  |      2 |      10  |
|  Mavis  |    1   | 1st    |     1  | 1st    |    3   | 3rd    |     2  |      3 |       7  |
|  john   |    5   | 4th    |     3  | 2nd    |    4   | 4th    |     4  |      4 |      15  |
|  bendo  |    2   | 2nd    |     5  | 3rd    |    2   | 2nd    |     1  |      2 |       7  |
+---------+--------+--------+--------+--------+--------+--------+--------+--------+----------+

请感谢所有帮助和建议..非常感谢

1 个答案:

答案 0 :(得分:0)

  

EG。如果james的结果是Grade1 = 4,Grade2 = 6,Grade3 = 1 ..代码必须   选择这三个等级中最好的两个等级   例如,Grade1 = 4,Grade3 = 1,并将结果添加到Grade4和   Grade5。

不是集中精力找到一种方法来选择两个最好的成绩,然后总结它们,而是更容易找到最差的成绩并从所有3的总和中减去它。

类似的东西:

SELECT grade1+grade2+grade3 - least( grade1, grade2, grade3 )
FROM .....

注意:LEAST( x,y,z,....)函数选择最少的表达式列表。

如果“最小值”由G2_Pos列确定(最大Gx_Pos确定最少Grade),则可能是这样的:

SELECT grade1+grade2+grade3 -
       CASE greatest( G1_Pos , G2_Pos , G3_Pos )
          WHEN G1_Pos THEN Grade1
          WHEN G2_Pos THEN grade2
          WHEN G3_Pos THEN grade3
       END
FROM .....

问题可能是相同的等级,例如,当会有:

+--------+---------+--------+--------+--------+--------+
| G1_Pos |  grade1 | G2_Pos | grade2 | G2_Pos | grade3 |
+--------+---------+--------+--------+--------+--------+
| 3rd    |      10 | 3rd    |      5 | 3rd    |     2  |
+--------+---------+--------+--------+--------+--------+

然后这个表达式将始终选择grade1,在本例中为10。