有这样的一个数据库表:
month       name        salary
1           张三         1000
2           张三         1500
1           李四         1200
2           李四         1700
1           王五         1300
2           王五         1800要求SQL查询结果未:
month       张三         李四           王五
1           1000         1200           1300
2           1500         1700           1800想不明白,请牛人指点迷津……

解决方案 »

  1.   

    列固定用MAX(DECODE
    select month,
    max(decode(name,'张三',salary,0)) as 张三,
    max(decode(name,'李四',salary,0)) as 李四,
    max(decode(name,'王五',salary,0)) as 王五
    from tab
    group by month不固定参考,经典帖子里的行专列
      

  2.   

    用case when也可.
    select month,
           max(case name when '张三' then salary else 0 end) 张三,
           max(case name when '李四' then salary else 0 end) 李四,
           max(case name when '王五' then salary else 0 end) 王五
    from tb
    group by month
      

  3.   

    假如name里面的还有其他不同的值,如:XX,YY等,又如何?
      

  4.   

    那就是列数不固定了
    参考http://topic.csdn.net/u/20100109/13/6a10c168-f190-4766-b838-adbf03c4ac7b.html?39335