sql语句··急

来源:百度知道 编辑:UC知道 时间:2024/06/07 08:33:51
怎么样把这样一个表
year month amonunt
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

同意上面的思路,不过应该写成这样:
select year,
sum(case when month = '1' then amount else '0' end) as m1,
sum(case when month = '2' then amount else '0' end) as m2,
sum(case when month = '3' then amount else '0' end) as m3,
sum(case when month = '4' then amount else '0' end) as m4
from table_name
group by year

还有的话一直写到十二月

select year,
max(case when month = 1 then amount else '0' end),
max(case when month = 2 then amount else '0' end),
max(case when month = 3 then amount else '0' end),
max(case when month = 4 then amount else '0' end)
from table_name
group by year