SQL数据修改

时间:2018-05-14 17:27:07

标签: mysql sql

我有一个StudentName,GPA,Semester

的视图
        Name     GPA    Semester
          x        3.15   1
          x        2.89   2
          y        3.87   1
          y        4.0    2

我想像这样修改

     Name semester1 GPA  semester 2 GPA
       x        3.15          2.89
       y        3.87           4.0


 Any help would be appreciated.
 Thanks

1 个答案:

答案 0 :(得分:3)

使用条件聚合:

select Name,
       max(case when Semester = 1 then GPA end) AS "semester1 GPA",
       max(case when Semester = 2 then GPA end) AS "semester3 GPA"
from table t
where Semester in (1, 2)
group by Name;