如下数据:  字段:   ID   NAME 
  数据:   01   小王
           01   校长
           01   组长
           02   米米
           02   嘟嘟现在我想得到如下的结果:  字段:   ID   NAME1  NAME2  NAME3
  数据:   01   小王   校长   组长
           02   米米   嘟嘟   注意一定是一行多列啊!
有高手吗, 请教!

解决方案 »

  1.   

    那样组合出来的是  一列啊,效果是:01  小王,校长,组长
    我现在想要的是如下的效果啊:
      字段: ID   NAME1     NAME2      NAME3
      数据: 01   小王        校长        组长
      

  2.   

    你轉換后的字段是否是固定的3個name字段?
      

  3.   

    with test as (
    select '01' col1, 'A' col2, '小王' col3 from dual union all
    select '01' col1, 'B' col2, '校长' col3 from dual union all
    select '01' col1, 'C' col2, '组长' col3 from dual union all
    select '02' col1, 'A' col2, '米米' col3 from dual union all
    select '02' col1, 'B' col2, '嘟嘟' col3 from dual
    )/*select col1, WM_CONCAT(col3)
    from test 
    group by col1 */select 
          col1,
          max(case when col2 = 'A' then col3 else '' end ) name1,
          max(case when col2 = 'B' then col3 else '' end ) name2,
          max(case when col2 = 'C' then col3 else '' end ) name3
    from test  group by col1   COL1 NAME1 NAME2 NAME3 
    01 小王 校长 组长 
    02 米米 嘟嘟   
      
      

  4.   

    SQL> with tab as (
      2  select '01' ID,'小王' NAME from dual union all
      3  select '01','校长' from dual union all
      4  select '01', '组长' from dual union all
      5  select '02', '米米' from dual union all
      6  select '02' , '嘟嘟'  from dual
      7  )
      8  select id,
      9   max(decode(rn,'1',name,'')) NAME1,
     10   max(decode(rn,'2',name,'')) NAME2,
     11   max(decode(rn,'3',name,'')) NAME3
     12  from (
     13   select id,name,
     14   row_number()over(partition by id order by rownum )rn
     15   from tab)
     16  group by id;ID NAME1  NAME2  NAME3
    -- ------ ------ ------
    01 小王   校长   组长
    02 米米   嘟嘟   SQL> 
      

  5.   

    厉害, 可以啊!
    row_number什么样的数据有这个函数啊?我知道的oracle, sql server
    还有别的吗?
      

  6.   

    WMSYS.WM_CONCAT 是连接, 不能分字段, 并且WMSYS.WM_CONCAT后的顺序不正确,错乱