oracle中合并列值 将一列的多个值合并成一行

    xiaoxiao2022-07-13  154

    大家平时在查询数据的时候,肯定会遇到需要将一列的多个值变成一行

    第一种情况:显示在同一行的同一列上

    1、新建一个表test

    可以参考:https://blog.csdn.net/heqiang525/article/details/90210326 

    里面的建表、插入数据的操作。结果如下图:

    2、selcect name, wmsys.wm_concat(type) type_sums from test group by name;

    其中wmsys.wm_concat(type) 也可以改成wmsys.wm_concat(score)

    结果如下:

    结果中的type_sums显示<CLOB>点击旁边的...也可以正常显示内容,但是感觉不爽!!!

    修改如下:

    select name, dbms_lob.substr(wmsys.wm_concat(type)) type_sums from test group by name;

    完美解决!!!结果如下:

    第二种情况:显示在同一行的不同列上

    select name, max(decode(type,'math',score,0)) as 数学, max(decode(type,'english',score,0)) as 英语, max(decode(type,'chinese',score,0)) as 语文  from test group by name;

    结果如下:

     

    最新回复(0)