Моя логика
- транспонирование столбцов в строки и получение столбца оценки путем объединения всех
- rank () для разбиения на студента для получения
the ranks of all the 5 subjects for each individual student
Схема (SQLite v3.26)
CREATE TABLE Table1
("student" TEXT(5), "physics" INTEGER, "chemistry" INTEGER, "maths" INTEGER, "history" INTEGER, "english" INTEGER)
;
INSERT INTO Table1
("student", "physics", "chemistry", "maths", "history", "english")
VALUES
('Brian', 78, 62, 100, 40, 50),
('Henry', 55, 72, 85, 22, 50)
;
Запрос
with cte as (
select student,'physics' as class,physics as score from Table1 union all
select student,'chemistry' as class,chemistry as score from Table1 union all
select student,'maths' as class,maths as score from Table1 union all
select student,'history' as class,history as score from Table1 union all
select student,'english' as class,english as score from Table1
)
select student,class,score,RANK() OVER (partition by student order by score desc) rnk
from cte;
| student | class | score | rnk |
| ------- | --------- | ----- | --- |
| Brian | maths | 100 | 1 |
| Brian | physics | 78 | 2 |
| Brian | chemistry | 62 | 3 |
| Brian | english | 50 | 4 |
| Brian | history | 40 | 5 |
| Henry | maths | 85 | 1 |
| Henry | chemistry | 72 | 2 |
| Henry | physics | 55 | 3 |
| Henry | english | 50 | 4 |
| Henry | history | 22 | 5 |
Просмотр на БД Fiddle