zoukankan      html  css  js  c++  java
  • 【SQL查询】合并行_listagg

    listagg

    1. 语法:

      listagg(measure_expr, 'delimiter') within group (order by expr) [over (partition by expr)]

    2. 功能说明:

      measure_expr:任何基于列的表达式

      delimiter:分割符

      order by expr:决定被拼接的顺序

      over (partition by expr): 表中所有内容按照expr进行分区处理

    3. 【示例1】:

    create or replace view v as(  
      select 500 population, 'China' nation ,'Guangzhou' city from dual union all  
      select 1500 population, 'China' nation ,'Shanghai' city from dual union all  
      select 500 population, 'China' nation ,'Beijing' city from dual union all  
      select 1000 population, 'USA' nation ,'New York' city from dual union all  
      select 500 population, 'USA' nation ,'Bostom' city from dual union all  
      select 500 population, 'Japan' nation ,'Tokyo' city from dual   
    );  
    
    select v.population, v.nation, listagg(v.city, ',') within group (order by city) over (partition by nation) as rank from v;

      结果:

      

      【示例2】:

      

    create or replace view v as(  
      select 500 population, 'China' nation ,'Guangzhou' city from dual union all  
      select 1500 population, 'China' nation ,'Shanghai' city from dual union all  
      select 500 population, 'China' nation ,'Beijing' city from dual union all  
      select 1000 population, 'USA' nation ,'New York' city from dual union all  
      select 500 population, 'USA' nation ,'Bostom' city from dual union all  
      select 500 population, 'Japan' nation ,'Tokyo' city from dual   
    );  
    
    select v.nation, listagg(v.city, ',') within group (order by city) as rank from v group by nation;

    :listagg在这里启动汇总的作用。sum将数值结果加在一起,而listagg是把字符串连接在一起。

     

  • 相关阅读:
    2020年捌月份生活随笔
    2020年柒月份生活随笔
    2020年陆月份生活随笔
    第二次:郑州银行杯|2019郑州国际马拉松
    第一次:海尔|2017年青岛马拉松
    专项测试技能和线上线下监控
    实用
    Oracle 数据库 有用的sql语句
    Qt demo
    springboot demo
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/12218517.html
Copyright © 2011-2022 走看看