请教一个sql笔试题

表名为table的表内容如下
year month value
2009 1 1.1
2009 2 1.2
2009 3 1.3
2009 4 1.4
2010 1 2.1
2010 2 2.2
2010 3 2.3
2010 4 2.4

要求查询结果为

year m1 m2 m3 m4

2009 1.1 1.2 1.3 1.4
2010 2.1 2.2 2.3 2.4

sql语句怎么写?

原先那个语句不对,正确的应该是下面这个

[code="sql"]
select year, decode(month,'1',value,null) m1,decode(month,'2',value,null) m2,decode(month,'3',value,null) m3,decode(month,'4',value,null) m4 from table group by year

[/code]

你用的是哪个数据库呢?

[code="sql"]

select year, decode(1,'m1',month,null),decode(2,'m2',month,null), decode(3,'m3',month,null), decode(4,'m4',month,null) from table group by year
[/code]
典型的行转列

[quote]select year, decode(1,'m1',month,null),decode(2,'m2',month,null), decode(3,'m3',month,null), decode(4,'m4',month,null) from table group by year [/quote]

这个是 oracle 的吧

pigswimming 应该是正解

[quote]decode[/quote]你去GOOGLE一下ORACLE的 decode 函数就知道了

[code="java"] decode(month,'1',value,null) m1[/code]
注释 decode作用:如果month这一列的值匹配为'1',则取出对应的value值作为查询结果中m1这一列的值 如果不匹配则设定默认值为null,当然你也可以设置其他默认值,比如0