怎么把这张表转换成下面的形式(一条sql):
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

解决方案 »

  1.   

    select year,replace(wm_concat(amount),',',' ') from tab group by year
      

  2.   

    不用 wm_concat 函数呢,应该怎么写?
      

  3.   


    不满足要求,这样的sql 分不出来 m1 ,m2 ,m3 ,m4 列
      

  4.   

    行转列,大致如下
    SELECT year,
    sum(DECODE(month,1,amount,0)) as m1,
    sum(DECODE(month,2,amount,0)) as m2,
    ......
    sum(DECODE(month,11,amount,0)) as m11,
    sum(DECODE(month,12,amount,0)) as m12
    from tab
    group by year
      

  5.   

    select year,max(decode(month,1,amount,null)) m1,
    max(decode(month,2,amount,null)) m2,
    max(decode(month,3,amount,null)) m3,
    max(decode(month,4,amount,null)) m4,
    max(decode(month,5,amount,null)) m5,
    max(decode(month,6,amount,null)) m6,
    max(decode(month,7,amount,null)) m7,
    max(decode(month,8,amount,null)) m8,
    max(decode(month,9,amount,null)) m9,
    max(decode(month,10,amount,null)) m10,
    max(decode(month,11,amount,null)) m11,
    max(decode(month,12,amount,null)) m12
    from t group by year
    不好意思,看错了
      

  6.   


    就只有1.2.3.4
    select Year,sum(case when month=1 then amount end) m1,
    sum(case when month=2 then amount end) m2,
    sum(case when month=3 then amount end) m3,
    sum(case when month=4 then amount end) m4
    from tb
    group by Year
      

  7.   

    select year,
           sum(decode(month, 1, amount, 0)) m1,
           sum(decode(month, 2, amount, 0)) m2,
           sum(decode(month, 3, amount, 0)) m3,
           sum(decode(month, 4, amount, 0)) m4
      from table_name
     group by year
      

  8.   

    select year,
    sum(decode(month,1,amout,0))m1,
    sum(decode(month,2,amout,0))m2,
    sum(decode(month,3,amout,0))m3,
    sum(decode(month,4,amout,0))m4
    from ymd group by year;
      

  9.   

    两种方法可以实现行转列: case when condition then value1 else value2 end  decode (字段,value,value1,value2)