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

     

  • 相关阅读:
    从虚拟地址,到物理地址(开PAE)
    无LoadLibrary获取指定模块基址
    练习
    Centos安装Python3及设置对应版本pip
    Varnish安装使用(初学)
    luogu P2463 [SDOI2008]Sandy的卡片 |二分+hash
    luogu P2852 [USACO06DEC]牛奶模式Milk Patterns |二分+hash
    luogu P4051 [JSOI2007]字符加密 |后缀数组(SA)
    弦图 学习笔记&
    luogu P1600 天天爱跑步 |树上差分+LCA
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/12218517.html
Copyright © 2011-2022 走看看