题目要求:
year month amount
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
Mysql语句
select year,
sum(case month when 1 then amount else 0 end) m1,
sum(case month when 2 then amount else 0 end) m2,
sum(case month when 3 then amount else 0 end) m3,
sum(case month when 4 then amount else 0 end) m4
from test
group by year