SQL - 不同类别的列
我是 SQL 新手.我有一个包含不同考试数据的数据库,例如:
I am new to SQL. I have a database with data for different exams, for example:
Student Test Grade
--------------------
St1 T1 A
St2 T1 B
St3 T1 B
St1 T2 B
St2 T2 B
St3 T2 A
St1 T3 A
St2 T3 C
St3 T3 B
然后,我想打印一份使用测试(T1、T2 和 T3)作为列的报告:
Then, I would like to print a report using the Tests (T1, T2 and T3) as columns:
Student T1 T2 T3
----------------------
St1 A B A
St2 B B C
St3 B A B
我尝试了不同的方法,但我一直不知道如何生成这样的打印输出.任何帮助表示赞赏!
I have tried different things, but I got stuck on how to produce such a printout. Any help is appreciated!
推荐答案
使用:
SELECT t.student,
MAX(CASE WHEN t.test = 'T1' THEN t.grade END) AS T1,
MAX(CASE WHEN t.test = 'T2' THEN t.grade END) AS T2,
MAX(CASE WHEN t.test = 'T3' THEN t.grade END) AS T3
FROM TABLE t
GROUP BY t.student
相关文章