有表A
year month num
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
现在要实现以下效果:
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
实现方法1:
select year,max(case when month=1 then num else 0 end)as m1,
max(case when month=2 then num else 0 end)as m2,
max(case when month=3 then num else 0 end)as m3,
max(case when month=4 then num else 0 end)as m4
from A group by year;