我通过 SQL语句统计数据select 
       b.sque,
       a.ques_id,
              case
         when b.type = '1' then
          (select c.answer from wj_option c where c.answer_id = a.answer)
         when b.type = '2' then
          (select c.answer from wj_option c where c.answer_id = a.answer)
         when b.type = '3' then
          a.answer
       end answer
  from wj_answer a, wj_ques b
 where a.ques_id = b.ques_id
      
   and a.staffno = '002030'
   and a.top_id = '162'
 order by a.top_id, a.ques_id;得到的数据格式如下:1   ss     aa
1   ss     bb
1   ss     cc
2   22     dd
3   dd     hh
3   dd     kk
怎么写SQL把数据变成这样的结构:
1   ss     aa,bb,cc
2   22     dd
3   dd     hh,kk
  
  

解决方案 »

  1.   

    Oracle 10g用wm_concat()函数,三种方法,详见:http://blog.csdn.net/gelyon/archive/2010/09/20/5897608.aspx
      

  2.   


    select sque,ques_id,wm_concat(answer) answers from 
    (select  
      b.sque,
      a.ques_id,
      case
      when b.type = '1' then
      (select c.answer from wj_option c where c.answer_id = a.answer)
      when b.type = '2' then
      (select c.answer from wj_option c where c.answer_id = a.answer)
      when b.type = '3' then
      a.answer
      end answer
      from wj_answer a, wj_ques b
     where a.ques_id = b.ques_id   
      and a.staffno = '002030'
      and a.top_id = '162'
     order by a.top_id, a.ques_id;
    )
    group by sque,ques_id
      

  3.   

    --wm_concat函数 或者自己写个函数游标遍历记录拼接下select sque,ques_id,wm_concat(answer)
    from (select  
      b.sque,
      a.ques_id,
      case
      when b.type = '1' then
      (select c.answer from wj_option c where c.answer_id = a.answer)
      when b.type = '2' then
      (select c.answer from wj_option c where c.answer_id = a.answer)
      when b.type = '3' then a.answer end answer
      from wj_answer a, wj_ques b
     where a.ques_id = b.ques_id
        
      and a.staffno = '002030'
      and a.top_id = '162'
     order by a.top_id, a.ques_id)
    group by sque,ques_id