I have test_scores
table with following fields:
Table schema:
id (number)
score1 (number)
score2 (number)
score3 (number)
score4 (number)
Sample data:
id score1 score2 score3 score4
1 10 05 30 50
2 05 15 10 00
3 25 10 05 15
Expected result set:
id col_name col_value
1 score4 50
2 score2 15
3 score1 25
What is a good SQL for this?(I am using MySQL.)
Original requirement didn't include the col names in the rowset. I was able to get the result using following SQL:
SELECT A.id, MAX(A.score) AS max_score
FROM (
SELECT id, score1 as score FROM test_scores UNION
SELECT id, score2 as score FROM test_scores UNION
SELECT id, score3 as score FROM test_scores UNION
SELECT id, score4 as score FROM test_scores
) AS A
GROUP BY A.id
But with the introduction of col_name in the result-set I am stumped..