zoukankan      html  css  js  c++  java
  • oracle 把一行用特殊符号分隔变为多行

    首先创建测试数据

    -- Create table
    create table UF_PQJZB
    (
      id                     INTEGER not null,
      requestid              INTEGER,
      pqmc                   VARCHAR2(100),
      sszq                   INTEGER,
      sfyx                   INTEGER,
      formmodeid             INTEGER,
      modedatacreater        INTEGER,
      modedatacreatertype    INTEGER,
      modedatacreatedate     VARCHAR2(10),
      modedatacreatetime     VARCHAR2(8),
      modedatamodifier       INTEGER,
      modedatamodifydatetime VARCHAR2(100),
      modeuuid               VARCHAR2(100),
      modelableid            VARCHAR2(200),
      pqbm                   VARCHAR2(99),
      qdbls                  INTEGER,
      pqdd                   CLOB
    )
    tablespace ECOLOGY
      pctfree 10
      initrans 1
      maxtrans 255
      storage
      (
        initial 64K
        next 1M
        minextents 1
        maxextents unlimited
      );
    -- Create/Recreate indexes 
    create index UF_IND_1595982945177 on UF_PQJZB (MODEUUID)
      tablespace ECOLOGY
      pctfree 10
      initrans 2
      maxtrans 255
      storage
      (
        initial 64K
        next 1M
        minextents 1
        maxextents unlimited
      );
    -- Create/Recreate primary, unique and foreign key constraints 
    alter table UF_PQJZB
      add primary key (ID)
      using index 
      tablespace ECOLOGY
      pctfree 10
      initrans 2
      maxtrans 255
      storage
      (
        initial 64K
        next 1M
        minextents 1
        maxextents unlimited
      );

    插入数据

    insert into uf_pqjzb (ID, AA)
    values (57, '5120,4940');

    sql显示

    SELECT * FROM 
    (
    with t AS (
    SELECT id,to_char(pqdd) AS aa from uf_pqjzb WHERE pqdd IS NOT NULL AND ID ='57' 
    )
    select ID,regexp_substr(aa, '[^,]+', 1, level) cc
     
    from t
     
    connect by level <= regexp_count(aa, '\,') + 1
     
    and aa = prior aa
     
    and prior dbms_random.value > 0
    )

    结果显示

     最后我们可以学一个行转列函数

    根据id合并

    SELECT ID , LISTAGG(cc, ',') WITHIN GROUP (ORDER BY ID)
      FROM (SELECT * FROM 
    (
    with t AS (
    SELECT id,to_char(pqdd) AS aa from uf_pqjzb WHERE pqdd IS NOT NULL AND ID ='57' 
    )
    select ID,regexp_substr(aa, '[^,]+', 1, level) cc
     
    from t
     
    connect by level <= regexp_count(aa, '\,') + 1
     
    and aa = prior aa
     
    and prior dbms_random.value > 0
    ))
     GROUP BY ID;

    结果

     好了,当然也可以用WM_CONCAT函数

    但是一定要注意WM_CONCAT转换为是clob字段需要注意

  • 相关阅读:
    09-异常处理-成绩判断异常
    继承与多态———动手动脑
    课下作业04-2String的使用方法
    课下作业04-1字符串加密
    课下作业03-2动手动脑及验证
    课下作业03-1请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?
    课下作业02-动手动脑
    Myschool试题
    使用ADO.NET
    模糊查询和聚合函数
  • 原文地址:https://www.cnblogs.com/chenzhelove/p/13485117.html
Copyright © 2011-2022 走看看