Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum for same ID in every row SQL

I am having table as below..

    r_Id     Marks
     1        25
     1        25   
     1        25
     2        30
     2        30  

now i want a query in which sum of marks for each r_id should be calculated and result should be

    r_Id  MARKS   Sum
     1      25    75   
     1      25    75
     1      25    75
     2      30    60 
     2      30    60

I need to do this in oracle without using PL/SQL. Please Help. I have tried using CUBE, ROLLUP, GROUPING SETS in GROUP BY, but nothing is working.

like image 293
Himanshu Avatar asked Dec 06 '25 19:12

Himanshu


1 Answers

You want to use the analytic functions for this. These are the functions that use an over clause:

select r_id, Marks, sum(Marks) over (partition by r_id)
from t;
like image 119
Gordon Linoff Avatar answered Dec 08 '25 09:12

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!