2017-05-26 89 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