用加法和减法计算累计和
Calculate cumulative sum with addition and subtraction
我有给定格式的 table
+----+--------------+-----------+-----------+-----------+------------+
| 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 |
+----+--------------+-----------+-----------+-----------+------------+
如图所示计算每个学生的累积列
累积 + 加分 - 每个学生的子分
您需要 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
Table 姓名: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;
我正在使用 window 函数 lag
从上一行获取值(这里是您需要排序的地方),并使用 coalesce
函数正确处理每一行的第一行学生,其中 lag
returns null 将其替换为 0,因为添加 null 将 return null。
输出
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_name
排序,那么累积会发生变化,因为 Adam | Chemistry
在 Adam | Physics
对之前排序。您可以在此示例中使用降序,但就添加的不同主题而言,这并不能真正解决您的问题。
Kamil 的调整解决方案:
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 ;
顺便说一句,输出就像 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
我有给定格式的 table
+----+--------------+-----------+-----------+-----------+------------+
| 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 |
+----+--------------+-----------+-----------+-----------+------------+
如图所示计算每个学生的累积列 累积 + 加分 - 每个学生的子分
您需要 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
Table 姓名: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;
我正在使用 window 函数 lag
从上一行获取值(这里是您需要排序的地方),并使用 coalesce
函数正确处理每一行的第一行学生,其中 lag
returns null 将其替换为 0,因为添加 null 将 return null。
输出
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_name
排序,那么累积会发生变化,因为 Adam | Chemistry
在 Adam | Physics
对之前排序。您可以在此示例中使用降序,但就添加的不同主题而言,这并不能真正解决您的问题。
Kamil 的调整解决方案:
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 ;
顺便说一句,输出就像 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