zoukankan      html  css  js  c++  java
  • listagg wm_concat 行转列

    一、

    这个写法和wm_concat相似,
    listagg(day,',')要把哪一列转换为同一行
    within group (order by day)同一行如何排序

    with temp as
     (
     select '1月' month, '1' day from dual union all
     select '2月' month, '1' day from dual union all
     select '2月' month, '2' day from dual union all
     select '3月' month, '1' day from dual union all
     select '3月' month, '2' day from dual union all
     select '3月' month, '3' day from dual 
     )
    select 
     month,listagg(day,',')within group (order by day ) days
     from temp 
     group by month;

     

    with temp as
     (
     select '1月' month, '1' day from dual union all
     select '2月' month, '1' day from dual union all
     select '2月' month, '2' day from dual union all
     select '3月' month, '1' day from dual union all
     select '3月' month, '2' day from dual union all
     select '3月' month, '3' day from dual 
     )
    select 
       month,
       to_char(wm_concat(day))
     from temp 
    group by month;

    二、

    不使用group by

    with temp as
     (
     select '1月' month, '1' day from dual union all
     select '2月' month, '1' day from dual union all
     select '2月' month, '2' day from dual union all
     select '3月' month, '1' day from dual union all
     select '3月' month, '2' day from dual union all
     select '3月' month, '3' day from dual 
     )
    select month , 
    listagg(day,',')within group (order by day) over (partition by month) DAYS
    from temp

    with temp as
     (
     select '1月' month, '1' day from dual union all
     select '2月' month, '1' day from dual union all
     select '2月' month, '2' day from dual union all
     select '3月' month, '1' day from dual union all
     select '3月' month, '2' day from dual union all
     select '3月' month, '3' day from dual 
     )
    select *
      from (select month,
                   to_char(wm_concat(day) over(partition by month order by day)) days,
                   row_number() over(partition by month order by day desc) rn
              from temp)
     where rn = 1

  • 相关阅读:
    SystemTap----常用变量、宏、函数和技巧
    RPM制作
    percona-MYSQLGUI监控
    Rsyslog配置文件详解
    理解 Linux 网络栈(1):Linux 网络协议栈简单总结 图
    tcp-backlog配置
    Clumsy logo差网络环境模拟工具 Clumsy
    Android Studio 配置模拟器AVD存放路径(默认在c盘,解决c盘空间不够问题)
    Android Studio 导入的项目编码错误问题
    21分钟 MySQL 入门教程
  • 原文地址:https://www.cnblogs.com/-beauTiFul/p/8663575.html
Copyright © 2011-2022 走看看