Posted on 28th April 2025|56 views
How to create a pivot table in the PostgreSQL?
Posted on 28th April 2025| views
Raw table
Student |
Subject |
Evaluation_result |
Smith, John |
Music |
7.0 |
Smith, John |
Maths |
4.0 |
Smith, John |
History |
9.0 |
Smith, John |
Language |
7.0 |
Smith, John |
Geography |
9.0 |
Gabriel, Peter |
Music |
2.0 |
Gabriel, Peter |
Maths |
10.0 |
Gabriel, Peter |
History |
7.0 |
Gabriel, Peter |
Language |
4.0 |
Gabriel, Peter |
Geography |
10.0 |
Pivot table
Student |
Geography |
History |
Language |
Maths |
Music |
Gabriel, Peter |
10.0 |
7.0 |
4.0 |
10.0 |
2.0 |
Smith, John |
9.0 |
9.0 |
7.0 |
4.0 |
7.0 |
Syntax:
SELECT student, subject FROM evaluations ORDER BY 1,2
AS final_result(Student TEXT, Geography NUMERIC,History NUMERIC,Language NUMERIC,Maths NUMERIC,Music NUMERIC)
SELECT * FROM crosstab( 'select student, subject from evaluations order by 1,2') AS final_result(Student TEXT, Geography NUMERIC,History NUMERIC,Language NUMERIC,Maths NUMERIC,Music NUMERIC); |