MySQL Question

MySQL : Getting the sum of multiple columns

This is very conflict for me let me explain to the best that I could please. This is like a 2 layers to compute. The first layer is I need to get the total A, and total B, and total C, and total D . But, to get the total A is i need to compute some columns, as well as total B, C and D . The second layer is to display and get the sum of the total A ,B, C, D. This is a bunch of tables.

For the first table.

tbl_criteria
------------------------
crit_id | criteria_name
16 | sports
17 | formal
18 | talent
19 | nothing


The tbl_criteria has a sub criteria

tbl_sub_criteria
----------------------
sub_crit_id | crit_id | sub_crit_name
22 | 16 | originality
23 | 16 | audience Impact
24 | 18 | Appeal
25 | 18 | Stage Presence


Third table the judges.

tbl_judges
------------------------
judge_id | judge_name
61 | first
62 | second
63 | third


Table of contestant lets say 2 contestant

tbl_cotestant
-----------------------------------------------
con_id | contestant_number | contestant_name |
1 | 1 | john |
2 | 2 | sy |


Last table, This is the constructed table

tbl_score
--------------------------------------------------
score_id | crit_id | sub_crit_id | judge_id | con_id | contestant_number | score
1 | 16 | 22 | 61 | 1 | 1 | 25
2 | 16 | 22 | 61 | 2 | 2 | 25
3 | 16 | 22 | 62 | 1 | 1 | 25
4 | 16 | 22 | 62 | 2 | 2 | 73
5 | 16 | 22 | 63 | 1 | 1 | 70
6 | 16 | 22 | 63 | 2 | 2 | 80
7 | 16 | 23 | 61 | 1 | 1 | 25
8 | 16 | 23 | 61 | 2 | 2 | 25
9 | 16 | 23 | 62 | 1 | 1 | 25
10 | 16 | 23 | 62 | 2 | 2 | 73
11 | 18 | 23 | 63 | 1 | 1 | 70
12 | 16 | 23 | 63 | 2 | 2 | 80
13 | 18 | 24 | 61 | 1 | 1 | 25
14 | 18 | 24 | 61 | 2 | 2 | 25
15 | 18 | 24 | 62 | 1 | 1 | 25
16 | 18 | 24 | 62 | 2 | 2 | 73
17 | 18 | 24 | 63 | 1 | 1 | 70
18 | 18 | 24 | 63 | 2 | 2 | 80
19 | 18 | 25 | 61 | 1 | 1 | 25
20 | 18 | 25 | 61 | 2 | 2 | 25
21 | 18 | 25 | 62 | 1 | 1 | 25
22 | 18 | 25 | 62 | 2 | 2 | 73
23 | 18 | 25 | 63 | 1 | 1 | 70
24 | 18 | 25 | 63 | 2 | 2 | 80
25 | 17 | null | 61 | 1 | 1 | 25
26 | 17 | null | 61 | 2 | 2 | 25
27 | 17 | null | 62 | 1 | 1 | 25
28 | 17 | null | 62 | 2 | 2 | 73
29 | 17 | null | 63 | 1 | 1 | 70
30 | 17 | null | 63 | 2 | 2 | 80
31 | 19 | null | 61 | 1 | 1 | 25
32 | 19 | null | 61 | 2 | 2 | 25
33 | 19 | null | 62 | 1 | 1 | 25
34 | 19 | null | 62 | 2 | 2 | 73
35 | 19 | null | 63 | 1 | 1 | 70
36 | 19 | null | 63 | 2 | 2 | 80


The first layer output are something like this, getting the total A, B, C ,D.

Total are should be display like

(criteria 16 has two sub-criterias 22, 23 , that means it will be x2)
con_num | contestant_name | 16_judge_61 | 16_judge_62 | 16_judge_63 | total a
1 | john | 50 | 25 | 140 | 215
2 | sy | 50 | 146 | 160 | 365


Table right above, John has got a total of 215 in Criteria number 16 (crit_id 16). As well as sy has got a total of 365 in criteria number 16.

So, i have 4 criteria in my table 16,17,18,19. This is my problem, that means i need to do query one by one to get each total output and that would be like

con_num | contestant_name | 17_judge_61 | 17_judge_62 | 17_judge_63 | total b
1 | john | 25 | 25 | 70 | 120
2 | sy | 25 | 73 | 80 | 178

(criteria 18 has a sub criteria 24,25 that means it will x2)
con_num | contestant_name | 18_judge_61 | 18_judge_62 | 18_judge_63 | total c
1 | john | 50 | 50 | 140 | 240
2 | sy | 50 | 146 | 160 | 356


con_num | contestant_name | 19_judge_61 | 19_judge_62 | 19_judge_63 | total d
1 | john | 25 | 25 | 70 | 120
2 | sy | 25 | 73 | 80 | 178


I need to do that in one query, I can do only if one by one getting total A, B C. But i need a one query that execute this kind of output. How can i achieve this output?

The total_a, b , c, d are equivalent to crit_id 16, 17, 18, 19

con_num | contestant_name | 16total_a | 17total_b | 18total_c | 19total_d | Grand_total
1 | john | 215 | 120 | 240 | 120 | 695
2 | jy | 365 | 178 | 356 | 178 | 1077


This query, I'm still learning about the logic

SELECT DISTINCT(a.contestant_number) as con_num, a.contestant_name,

//Getting first the sum of total a, Notice criteria 16 has a sub criteria 22, 23
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 22 AND s.judge_id='61' THEN s.score END) as 16_judge_61,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 22 AND s.judge_id='62' THEN s.score END) as 16_judge_62,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 22 AND s.judge_id='63' THEN s.score END) as 16_judge_63,

SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 23 AND s.judge_id='61' THEN s.score END) as 16_judge_61,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 23 AND s.judge_id='62' THEN s.score END) as 16_judge_62,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 23 AND s.judge_id='63' THEN s.score END) as 16_judge_63,

SUM(CASE WHEN s.crit_id='16' AND s.judge_id in (61, 62, 63) THEN s.score END) as 'total a'

//Criteria 17 has no sub criteria
SUM(CASE WHEN s.crit_id='17' AND s.judge_id='61' THEN s.score END) as 16_judge_61,
SUM(CASE WHEN s.crit_id='17' AND s.judge_id='62' THEN s.score END) as 16_judge_62,
SUM(CASE WHEN s.crit_id='17' AND s.judge_id='63' THEN s.score END) as 16_judge_63,


SUM(CASE WHEN s.crit_id='17' AND s.judge_id in (61, 62, 63) THEN s.score END) as 'total b'

//And iterate again for the 2 more criteria
//
FROM tbl_score s
INNER JOIN tbl_contestant a ON s.contestant_number = a.contestant_number
INNER JOIN tbl_judges j ON j.judge_id = s.judge_id
WHERE c.gender = 'male' and c.con_id = s.con_id
GROUP BY s.contestant_number
ORDER By `Grand toal' DESC";


http://sqlfiddle.com/#!9/cfe90/1

Getting the sum of only one total
http://sqlfiddle.com/#!9/9efa5/1

Answer

Put your query in a subquery, and then add together the columns to get the grand total.

SELECT x.*, `total a` + `total b` + `total c` AS `Grand Total`
FROM (put your query here) AS x
Comments