如果你的 1类          2类        3类
是数值类型的话,可以这样
select id, sum(1类),sum(2类),sum(3类)
from (
select id, 1类 ,0,0 from 
tab_1  where lx=1类
union all 
select id, 0,2类,0 from 
tab_1  where lx=2类
union all 
select id, 0 ,0,3类 from 
tab_1  where lx=3类
) as temp
group by id

解决方案 »

  1.   

    selec id,(select sj from tab_l a where a.id=b.id and lx='1类') as 1类,
    (select sj from tab_l a where a.id=b.id and lx='2类') as 2类,
    (select sj from tab_l a where a.id=b.id and lx='3类') as 3类
    from tab_l b
      

  2.   

    selec id,(select sj from tab_l a where a.id=b.id and lx='1类') as 1类,
    (select sj from tab_l a where a.id=b.id and lx='2类') as 2类,
    (select sj from tab_l a where a.id=b.id and lx='3类') as 3类
    from tab_l b
      

  3.   

    问题就是我的lx字段是字符型的不是数值型,另外楼上的朋友,你的table b什么意思??从何而来
      

  4.   

    select id,max(decode(lx,'1类',sj)) 1类,
              max(decode(lx,'2类',sj)) 2类,
              max(decode(lx,'3类',sj)) 3类
    from tab_1  
    group by id;
      

  5.   

    select id,max(decode(lx,'1类',sj)) 1类,
              max(decode(lx,'2类',sj)) 2类,
              max(decode(lx,'3类',sj)) 3类
    from tab_1  
    group by id;
      

  6.   

    select id,max(decode(lx,'1类',sj)) 1类,
              max(decode(lx,'2类',sj)) 2类,
              max(decode(lx,'3类',sj)) 3类
    from tab_1  
    group by id;
      

  7.   

    谢了斑竹,我想问一下这个decode是什么意思???
      

  8.   

    http://gigabase.idi.ntnu.no/oradoc/server.901/a90125/functions33.htm#1017439