我有一张桌子
s_id student_name attendance_date status
1 student1 2020-01-01 P
2 student1 2020-01-02 P
3 student2 2020-01-01 P
4 student2 2020-01-02 A
我希望我的桌子是
s_id student_name 01-01-2020 02-01-2020
1 student1 P P
2 student2 P A
我已经尝试使用CASE WHEN语句,但学生的名字一直在重复。我如何才能得到不重复的学生名字的结果?
为了在固定的日期列表上进行透视,你可以进行条件聚合。
select
s_id,
student_name,
max(case when attendance_date = '2020-01-01' then status end) `2020-01-01`,
max(case when attendance_date = '2020-01-02' then status end) `2020-01-02`
from mytable
group by s_id, student_name
order by s_id
试试下面,这里是 演示. 我没有包括 s_id
因为在这里加入它没有任何意义。
select
student_name,
max(case when attendance_date='2020-01-01' then status end) as '01-01-2020',
max(case when attendance_date='2020-01-02' then status end) as '01-02-2020'
from yourTable
group by
student_name