clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2335534 fiddles created (27396 in the last week).

CREATE TABLE grades(student text, subject text, grade float);
 hidden batch(es)


INSERT INTO grades VALUES ('Alice', 'Geography', 4.0), ('Alice', 'History', 4.5), ('Alice', 'Math', 4.0), ('Bob', 'Geography', 3.0), ('Bob', 'History', 3.0), ('Bob', 'Math', 3.5), ('Charlie', 'Geography', 4.5), ('Charlie', 'History', 4.0), ('Charlie', 'Math', 3.0);
9 rows affected
 hidden batch(es)


SELECT * FROM grades
student subject grade
Alice Geography 4
Alice History 4.5
Alice Math 4
Bob Geography 3
Bob History 3
Bob Math 3.5
Charlie Geography 4.5
Charlie History 4
Charlie Math 3
 hidden batch(es)


SELECT * FROM crosstab('SELECT student, subject, grade FROM grades order by 1,2') AS final_result(Student TEXT, Geography FLOAT, History FLOAT, Math Float);
student geography history math
Alice 4 4.5 4
Bob 3 3 3.5
Charlie 4.5 4 3
 hidden batch(es)


SELECT student, name, value FROM grades JOIN LATERAL (VALUES('subject', grades.subject), ('grade', cast(grade AS text))) s(name, value) ON true
student name value
Alice subject Geography
Alice grade 4
Alice subject History
Alice grade 4.5
Alice subject Math
Alice grade 4
Bob subject Geography
Bob grade 3
Bob subject History
Bob grade 3
Bob subject Math
Bob grade 3.5
Charlie subject Geography
Charlie grade 4.5
Charlie subject History
Charlie grade 4
Charlie subject Math
Charlie grade 3
 hidden batch(es)