在 SQL 查询中添加一个只应过滤结果的列
Add a column in an SQL query that should only filtered results
我有以下 table:
TABLE
+--------------------------+
| STUDENT CLASS SCORE |
+--------------------------+
| Student1 Science Pass |
| Student1 Math Fail |
| Student1 Geography Fail |
| Student2 Science Pass |
| Student2 Math Pass |
| Student2 Geography Fail |
+--------------------------+
我希望看到如下结果
+------------------------------------+
| STUDENT Science Math Geography |
+------------------------------------+
| Student1 Pass Fail Fail |
| Student2 Pass Pass Fail |
+------------------------------------+
知道我该怎么做吗?
对于 类 的固定列表,您可以只使用条件聚合:
select student,
max(case when class = 'Science' then score end) as science,
max(case when class = 'Math' then score end) as math,
max(case when class = 'Geography' then score end) as geography
from mytable
group by student
我有以下 table:
TABLE
+--------------------------+
| STUDENT CLASS SCORE |
+--------------------------+
| Student1 Science Pass |
| Student1 Math Fail |
| Student1 Geography Fail |
| Student2 Science Pass |
| Student2 Math Pass |
| Student2 Geography Fail |
+--------------------------+
我希望看到如下结果
+------------------------------------+
| STUDENT Science Math Geography |
+------------------------------------+
| Student1 Pass Fail Fail |
| Student2 Pass Pass Fail |
+------------------------------------+
知道我该怎么做吗?
对于 类 的固定列表,您可以只使用条件聚合:
select student,
max(case when class = 'Science' then score end) as science,
max(case when class = 'Math' then score end) as math,
max(case when class = 'Geography' then score end) as geography
from mytable
group by student