一个表A中有三个字段:字段a,字段b,字段c。存放数据如下:
字段a   字段 b   字段c
a       m        1
a       n        2
a       p        3
b       m        3
b       n        5
现在,想要得到这样的结果:
字段a   m   n   p
a       1   2   3
b       3   5求这样的语句怎么写?谢谢!

解决方案 »

  1.   

    查下行转列select a, 
           decode(sum(case when b = 'm' then c else 0),0,null,sum(case when b = 'm' then c else 0)) m,
           decode(sum(case when b = 'n' then c else 0),0,null,sum(case when b = 'n' then c else 0)) n,
           decode(sum(case when b = 'p' then c else 0),0,null,sum(case when b = 'p' then c else 0)) p
    from   A
    group by a;
      

  2.   

    select a, sum(decode(b, 'm', c, 0)) m, sum(decode(b, 'n', c, 0)) n, sum(decode(b, 'p', c, 0)) p from a group by a
      

  3.   

    这个有NULL,好像更可取一些。