I want do some calculations for the column total
by adding the two marks columns for the specific
std_id
.
For example 30+25=55
, total=55 for std_id 32
+--------+-----------+-------+----+----------------+-------+
| std_id | course_id | marks | mp | subject | total |
+--------+-----------+-------+----+----------------+-------+
| 32 | 40 | 30.00 | 22 | Elective Maths | 0 |
| 32 | 40 | 25.00 | 12 | Elective Maths | 0 |
| 39 | 40 | 30.00 | 22 | Elective Maths | 0 |
| 39 | 40 | 20.00 | 12 | Elective Maths | 0 |
+--------+-----------+-------+----+----------------+-------+
I tried something like
update student_reports
set total= sum(marks)
where std_id=std_id;
but does not work, any help is greatly appreciated especially one that does not require changing the table structure
For the first table above, with a single course_id, the code by GILEYS calculated the total perfectly
update student_reports r, ( select std_id, sum(marks) as total from student_reports group by std_id ) t set r.total = t.total where r.std_id = t.std_id;
My next question is what if i add another course_id value, to make it (40,43) to the table, how can i do the new totals
+--------+-----------+-------+----+----------------+-------+
| std_id | course_id | marks | mp | subject | total |
+--------+-----------+-------+----+----------------+-------+
| 32 | 40 | 30.00 | 22 | Elective Maths | 0 |
| 32 | 40 | 25.00 | 12 | Elective Maths | 0 |
| 39 | 40 | 30.00 | 22 | Elective Maths | 0 |
| 39 | 40 | 20.00 | 12 | Elective Maths | 0 |
| 32 | 43 | 40.00 | 22 | Core Maths | 0 |
| 32 | 43 | 20.00 | 12 | Core Maths | 0 |
| 39 | 43 | 45.00 | 22 | Core Maths | 0 |
| 39 | 43 | 40.00 | 12 | Core Maths | 0 |
+--------+-----------+-------+----+----------------+-------+
I tried something like
update student_reports r, (select std_id,course_id,subject, sum(marks) as total from student_reports group by std_id) t set r.total = t.total where r.std_id = t.std_id AND r.course_id=t.course_id AND r.subject=t.subject;
but is not giving out the totals as i would expert, please need your help again
to achieve the desired results below
+--------+-----------+-------+----+----------------+-------+
| std_id | course_id | marks | mp | subject | total |
+--------+-----------+-------+----+----------------+-------+
| 32 | 40 | 30.00 | 22 | Elective Maths | 55 |
| 32 | 40 | 25.00 | 12 | Elective Maths | 55 |
| 39 | 40 | 30.00 | 22 | Elective Maths | 50 |
| 39 | 40 | 20.00 | 12 | Elective Maths | 50 |
| 32 | 43 | 40.00 | 22 | Core Maths | 60 |
| 32 | 43 | 20.00 | 12 | Core Maths | 60 |
| 39 | 43 | 45.00 | 22 | Core Maths | 95 |
| 39 | 43 | 40.00 | 12 | Core Maths | 95 |
+--------+-----------+-------+----+----------------+-------+