zoukankan      html  css  js  c++  java
  • Oracle中的列转行例子详解

    数据如下:
    name id
    张三 1,2,3

    要求实现:
    name id
    张三 1
    张三 2
    张三 3

    --创建临时表
    create table tmp as(select '张三' name, '1,2,3' id from dual);
    --写法1
    select name,regexp_substr(id,'[^,]+',1,level) as id
    from tmp
    connect by level <= regexp_count(id,',')+1
    order by id
    
    --写法2:
    select name,trim(regexp_substr(id,'[^,]+',1,level)) as id
    from tmp
    connect by name = prior name 
    and prior dbms_random.value is not null
    and level <= length(regexp_replace(id, '[^,]'))+1;
    
    --写法3
    select name,
    --regexp_replace(id,'(w+)\,(w+)\,(w+)',level) id
    regexp_replace(id,'(w+)\,(w+)\,(w+)',''||to_char(level)) id
    from tmp
    connect by level <= regexp_count(id,',')+1;
    
    --写法4: 
    select name,
    substr(','||id||',',instr(','||id||',', ',', 1, level) + 1, instr(','||id||',', ',', 1, level + 1) -( instr(','||id||',', ',', 1, level) + 1))
    from tmp
    connect by level <= regexp_count(id,',')+1
    order by level

    此外,列转行还可以使用union all和unpivot(oracle 11g新特性)等,待后续补充

  • 相关阅读:
    cae when分组统计
    查看 Chrome 下载的文件的真实下载地址
    directory opus使用教程
    文件内容极速搜索工具: silversearcher-ag
    LINUX SHELL 变量的二次引用
    JS小练习
    jQuery
    JS-BOM对象
    JS-DOM对象
    JavaScript-基础知识
  • 原文地址:https://www.cnblogs.com/huangbiquan/p/7783080.html
Copyright © 2011-2022 走看看