2017-05-26 131 views
0

我有一个表中给定的格式计算加减累加和

+----+--------------+-----------+-----------+-----------+------------+ 
| 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 | 
+----+--------------+-----------+-----------+-----------+------------+ 

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

+1

'select name,subject,add_marks - 来自表的子标记;'?确切的问题是什么? – freakish

+0

不是这样。 OP需要计算每个学生分数的累计总和。 –

+0

我投票结束这个问题作为题外话,因为这是一个工作请求 –

回答

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 
+0

感谢@Kamil提供的基本解决方案。 – Alice

+0

感谢@Dan,更新后的解决方案完成了这个诀窍 – Alice

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摆脱以前行的值(在这里你需要排序)和​​3210功能,妥善处理第一行对于每个学生,lag返回null以将其替换为0,因为使用null添加将返回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对。在这个例子中,你可以使用降序,但是就不同的主题被添加而言,这并不能真正解决你的问题。卡米尔

+0

感谢您的解决方案。给定的解决方案对于给定的例子工作正常,但是如果我为Adam增加1行,输出是不正确的。请检查我编辑的问题 – Alice