在一个表中,如果对于A列出现重复的值,就把A列中出现重复的值对应的B列的值合并,请问怎么编写sql语句来实现?举个例子,有如下表:
studentID   course     score
   001       math       82
   002      english     68
   002      chinese     92
   003      physics     100
   004      bioloy      75
   005      physics     93
   004      chinese     86
   004      english     93执行sql语句后,能实现以下的查询结果
studentID   course                    score
   001      math                       82
   002      english,chinese            68,92
   003      physics                    100   
   004      bioloy,chinese,english     75,86,93
   005      physics                    93请问怎么编写sql语句?

解决方案 »

  1.   

    建议楼主去看一下 oracle 行列转换方面的文档!
      

  2.   

    create table tb(studentID varchar2(10), course varchar2(20), score number(18,2));insert into tb(studentID,course,score) values('001', 'math', 82);
    insert into tb(studentID,course,score) values('002', 'english', 68);
    insert into tb(studentID,course,score) values('002', 'chinese', 92);
    insert into tb(studentID,course,score) values('003', 'physics', 100);
    insert into tb(studentID,course,score) values('004', 'bioloy', 75);
    insert into tb(studentID,course,score) values('005', 'physics', 93);
    insert into tb(studentID,course,score) values('004', 'chinese', 86);
    insert into tb(studentID,course,score) values('004', 'english', 93);col course for a45;
    col score for a45;
    select  studentID, wm_concat(course) as course, wm_concat(to_char(score)) as score
    from (select studentID, course, score from tb order by studentID, score) t
    group by t.studentID;
      

  3.   

    SQL> col course for a45;
    SQL> col score for a45;
    SQL> select  studentID, wm_concat(course) as course, wm_concat(to_char(score)) as score
      2  from (select studentID, course, score from tb order by studentID, score) t
      3  group by t.studentID;\
      4
    SQL> col course for a45;
    SQL> col score for a45;
    SQL> select  studentID, wm_concat(course) as course, wm_concat(to_char(score)) as score
      2  from (select studentID, course, score from tb order by studentID, score) t
      3  group by t.studentID;STUDENTID            COURSE                                        SCORE
    -------------------- --------------------------------------------- ---------------------------------------------
    001                  math                                          82
    002                  english,chinese                               68,92
    003                  physics                                       100
    004                  bioloy,chinese,english                        75,86,93
    005                  physics                                       93
      

  4.   

    看不明白,我需要的是在任何一种数据库都能执行的通用的sql语句,不局限于oracle
      

  5.   

    把 这个函数wm_concat 重写的话就跟oracle没关系了  纯sql问题
      

  6.   

    wm_concat这个是什么函数?col是什么
    那个函数是怎么定义的?
      

  7.   


    如果楼主还在问这样的问题的话,建议您Oracle 从零学起!
      

  8.   

    函数内容就像这样就行了 ,把  @studentID 作为参数传进去  DECLARE   @FmtStr   VARCHAR(4000)  
      SET @FmtStr=''
      SELECT @FmtStr=@FmtStr+','+ course  FROM   tb WHERE studentID =@studentID 
       RETURN STUFF(@FmtStr,1,1,'')