用加法和减法计算累积和

时间:2017-05-26 15:23:48

标签: postgresql cumulative-sum

我有一个给定格式的表

+----+--------------+-----------+-----------+-----------+------------+
| ID | Student Name | Subject   | Add Marks | Sub Marks | Cumulative |
+----+--------------+-----------+-----------+-----------+------------+
| 1  | Adam         | Physics   |        74 |        15 |         59 |
+----+--------------+-----------+-----------+-----------+------------+
| 2  | Adam         | Chemistry |        62 |        11 |        110 |
+----+--------------+-----------+-----------+-----------+------------+
| 3  | Adam         | Maths     |       100 |        10 |        200 |
+----+--------------+-----------+-----------+-----------+------------+
| 4  | Joel         | Maths     |        90 |        10 |         80 |
+----+--------------+-----------+-----------+-----------+------------+
| 5  | Joel         | Physics   |        80 |        15 |        145 |
+----+--------------+-----------+-----------+-----------+------------+
| 6  | Joel         | Chemistry |        65 |        20 |        190 |
+----+--------------+-----------+-----------+-----------+------------+
| 7  | Zampa        | Physics   |        60 |        15 |         45 |
+----+--------------+-----------+-----------+-----------+------------+

计算每个学生的累积列,如图所示 累积+添加标记 - 每个学生的子标记

2 个答案:

答案 0 :(得分:0)

您需要ORDER BY的密钥才能达到预期的累计金额。 例如,我已为每条记录分配了id列,并将其从1开始递增1,因此示例数据如下所示:

 id | student_name |  subject  | addmark | submark
----+--------------+-----------+---------+--------
  1 | Adam         | Physics   |      74 |      15
  2 | Adam         | Chemistry |      62 |      11
  3 | Joel         | Maths     |      90 |      10
  4 | Joel         | Physics   |      80 |      15
  5 | Zampa        | Physics   |      60 |      15

表名:students

然后查询看起来像:

select
  student_name,
  subject,
  addmark,
  submark,
  addmark - submark
          + coalesce(lag(addmark - submark) over (partition by student_name order by id),0) as cumulative
from students;

我使用窗口函数lag来获取上一行的值(这里是您需要排序的位置)和coalesce函数来正确处理每个学生的第一行,其中{ {1}}返回null以将其替换为0,因为使用null添加将返回null。

<强>输出

lag

注意:如果没有 student_name | subject | addmark | submark | cumulative --------------+-----------+---------+---------+------------ Adam | Physics | 74 | 15 | 59 Adam | Chemistry | 62 | 11 | 110 Joel | Maths | 90 | 10 | 80 Joel | Physics | 80 | 15 | 145 Zampa | Physics | 60 | 15 | 45 列,我们会决定按id排序,那么累积会发生变化,因为student_nameAdam | Chemistry对之前排序。你可以在这个例子中使用降序,但是根据添加的不同主题,这不会真正解决你的情况。

答案 1 :(得分:0)

卡米尔改编解决方案:

select
  Student_Name,
  Subject,
  Add_Marks,
  Sub_Marks,
  sum(Add_Marks - Sub_Marks)
    over (partition by Student_Name order by ID) as Cumulative
from students;

如果没有ID字段,可以使用以下命令为此查询创建一个

select
  Student_Name,
  Subject,
  Add_Marks,
  Sub_Marks,
  sum(Add_Marks - Sub_Marks)
    over (partition by Student_Name order by ID) as Cumulative
from ( select *,row_number() over () as ID from students ) tmpt ;

输出BTW就像OP:

student_name      subject   add_marks   sub_marks   cumulative  
Adam    Physics     74  15  59
Adam    Chemistry   62  11  110
Adam    Maths       100 10  200
Joel    Maths       90  10  80
Joel    Physics     80  15  145
Joel    Chemistry   65  20  190
Zampa   Physics     60  15  45