mercredi 6 avril 2016

SQL get highest score of three columns does not return required fields

I am trying to display the highest score from three columns and order them as such, it does order them by highest score per column but does not return the surname and forename. Below is a segment of the code.

import sqlite3
connection = sqlite3.connect("class1.db")
cursor = connection.cursor()
cursor.execute('''SELECT score1, score2, score3,
    CASE
    WHEN score1 >= score2 AND score1 >= score3 THEN score1
    WHEN score2 >= score1 AND score2 >= score3 THEN score2
    WHEN score3 >= score1 AND score3 >= score2 THEN score3
    ELSE score1
    END AS high_score FROM class1 ORDER BY high_score DESC;''' )
print("Class 1:")
result = cursor.fetchall() 
for r in result:
    print(r)

This is the data it should return:

Class 1:
('Python', 'Monty', 5, 10, 7)

This is the data it returns:

Class 1:
(5, 10, 7, 10)

I have no idea what the problem is and I am not brilliant with SQL syntax, any help would be appreciated. The columns containing 'Python' and 'monty' are called Surname and Forename respectively.

Aucun commentaire:

Enregistrer un commentaire