面试题:怎么把这样一个表儿
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
create table mes (year varchar(22), mouth varchar(22), amount double);
insert into mes values('1991','1',1.1);
insert into mes values('1991','2',1.2);
insert into mes values('1991','3',1.3);
insert into mes values('1991','4',1.4);
insert into mes values('1992','1',2.1);
insert into mes values('1992','2',2.2);
insert into mes values('1992','3',2.3);
insert into mes values('1992','4',2.4);
select m.year,
(select m1.amount from mes m1 where m1.mouth='1' and m1.year = m.year) a1 ,
(select m2.amount from mes m2 where m2.mouth='2' and m2.year = m.year) a2 ,
(select m3.amount from mes m3 where m3.mouth='3' and m3.year = m.year) a3 ,
(select m4.amount from mes m4 where m4.mouth='4' and m4.year = m.year) a4
from mes m group by year;