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是把字符串连接在一起。

     

  • 相关阅读:
    使用IDEA整合SSM框架
    宏任务与微任务
    setTimeout的实现及其问题
    JS的闭合(Closure)
    this详解
    JS的作用域和作用域链
    JS的执行上下文
    JS内存机制
    抽象工厂模式(c++实现)
    迭代器模式(c++实现)
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/12218517.html
Copyright © 2011-2022 走看看