比如我有一张表 A 如下:
号码             联系人
13500000001       小张 
13500000001       小赵
13500000001       小钱
请问:如果要达到下面这样的效果这样写SQL语句?
号码              联系人
13500000001   小张|小赵|小钱

解决方案 »

  1.   

    SELECT REPLACE(WM_CONCAT(联系人), ',', '|') 
       FROM 表
       GROUP BY 号码
      

  2.   

    SELECT 号码,REPLACE(WM_CONCAT(联系人), ',', '|') 
      FROM 表 
      GROUP BY 号码
      

  3.   

    SELECT 号码, MAX(substr(sys_connect_by_path(联系人, '|'), 2)) str
    from A
      

  4.   

    with tt as(select 111 id, 'aaa' code from dual 
    union all select 111,'bbb' from dual 
    union all select 111,'ccc' from dual 

    select id,max(decode(rn,1,code,0))||'|'||max(decode(rn,2,code,0))||'|'||
    max(decode(rn,3,code,0))code3
    from(select tt.*,row_number()over(partition by id order by code)rn from tt) 
    group by id
      

  5.   

    with tt as(select 13500000001 号码, '小张' 联系人 from dual 
    union all select 13500000001,'小赵' from dual 
    union all select 13500000001,'小钱' from dual 

    select 号码,max(decode(rn,1,联系人,0))||'|'||max(decode(rn,2,联系人,0))||'|'||
    max(decode(rn,3,联系人,0))联系人
    from(select tt.*,row_number()over(partition by 号码 order by 联系人)rn from tt) 
    group by 号码
      

  6.   

    select 号码,relplace(wm_concat(联系人),',','|')
    from table1
    group by 号码