zoukankan      html  css  js  c++  java
  • oracle 聚合函数 LISTAGG ,将多行结果合并成一行

    LISTAGG(列名,' 分割符号')

    oracle 11g 以上的版本才有的一个将指定列名的多行查询结果,用 指定的分割符号 合并成一行显示:

    例如:

    表原始数据:

    需求:将 mb1_Transport_License_list 表中的数据,根据 transportation_license_id 数据进行分组,对 Item_Category_Name 列的数据进行 去重合并

    使用聚合函数 LISTAGG  解决

    [sql] view plain copy
     
    1. SELECT transportation_license_id,  
    2.      LISTAGG( to_char(Item_Category_Name), ',') WITHIN GROUP(ORDER BY Item_Category_Name) AS employees  
    3.       FROM ( select distinct transportation_license_id, item_category_name from mb1_Transport_License_list  ) group by transportation_license_id  

    SQL解析:

    select distinct transportation_license_id, item_category_name from mb1_Transport_Lincense_list ; -- 对需要做合并处理的数据源数据进行去重处理,如果实际要求不需要去重处理,这里可以直接改为  表名,(例如:  from  mb1_Transport_Lincense_list)进行查询

    LISTAGG( to_char(Item_Category_Name), ',') WITHIN GROUP(ORDER BY Item_Category_Name)  -- 将 Item_Category_Name 列的内容以", "进行分割合并、排序;

    to_char(Item_Category_Name) --  to_char(列名)  解决使用聚合函数 LISTAGG 进行查询后,对查询结果乱码问题进行转码处理;

    运行后的结果:

     
  • 相关阅读:
    iperf/iperf3网络测试工具的安装与使用
    驱动模块(4)——模块编译
    760. Find Anagram Mappings
    MySQL面试题
    MySQL 的数据存储引擎
    203. Remove Linked List Elements
    数据库事务隔离级别
    232. Implement Queue using Stacks
    MySQL中的事务
    482. License Key Formatting
  • 原文地址:https://www.cnblogs.com/mingjing/p/8980059.html
Copyright © 2011-2022 走看看