Oracle逗号分隔到具有分组的行

时间:2018-01-30 12:06:04

标签: oracle

我有以下要求。     我们是否在oracle 12c中有直接功能来完成此任务。

create table t1(input_name varchar2(500),input_values varchar2(500));
insert into t1 values('a,b,c,d,','1,2,3,4');
insert into t1 values('e,f,g,','5,6,7');
insert into t1 values('a1,b1,c1,d1,','11,12,13,14');
insert into t1 values('d,c,b,a,','100,200,300,400');
commit;

select * from t1;
INPUT_NAME                     INPUT_VALUES
------------------------------ ----------------
a,b,c,d,                       1,2,3,4
e,f,g,                         5,6,7
a1,b1,c1,d1,                   11,12,13,14
d,c,b,a,                       100,200,300,400

output:
a   b   c   d   e f g a1 b1 c1 d1
1   2   3   4   5 6 7 11 12 13 14
400 300 200 100

谢谢, 拉赫马特·阿里

1 个答案:

答案 0 :(得分:1)

是...如果您有一组已知的输入名称。但是,您最好重新组织数据,这样就不会存储相关的逗号分隔列表对。

SQL Fiddle

Oracle 11g R2架构设置

create table t1(input_name,input_values) AS
SELECT 'a,b,c,d,','1,2,3,4' FROM DUAL UNION ALL
SELECT 'e,f,g,','5,6,7' FROM DUAL UNION ALL
SELECT 'a1,b1,c1,d1,','11,12,13,14' FROM DUAL UNION ALL
SELECT 'd,c,b,a,','100,200,300,400' FROM DUAL
/
CREATE TYPE pair IS OBJECT(
  name  VARCHAR2(20),
  value VARCHAR2(20)
)
/

CREATE TYPE pair_table IS TABLE OF PAIR
/

查询1

SELECT MAX( CASE name WHEN 'a' THEN value END ) AS a,
       MAX( CASE name WHEN 'b' THEN value END ) AS b,
       MAX( CASE name WHEN 'c' THEN value END ) AS c,
       MAX( CASE name WHEN 'd' THEN value END ) AS d,
       MAX( CASE name WHEN 'e' THEN value END ) AS e,
       MAX( CASE name WHEN 'f' THEN value END ) AS f,
       MAX( CASE name WHEN 'g' THEN value END ) AS g,
       MAX( CASE name WHEN 'a1' THEN value END ) AS a1,
       MAX( CASE name WHEN 'b1' THEN value END ) AS b1,
       MAX( CASE name WHEN 'c1' THEN value END ) AS c1,
       MAX( CASE name WHEN 'd1' THEN value END ) AS d1
FROM   (
  SELECT v.name,
         v.value,
         ROW_NUMBER() OVER ( PARTITION BY v.name ORDER BY ROWNUM ) AS rn
  FROM   t1 t
         CROSS JOIN
         TABLE(
           CAST(
             MULTISET(
               SELECT pair(
                        REGEXP_SUBSTR( t.input_name,   '([^,]+)(,|$)', 1, LEVEL, NULL, 1 ),
                        REGEXP_SUBSTR( t.input_values, '([^,]+)(,|$)', 1, LEVEL, NULL, 1 )
                      )
               FROM   DUAL
               CONNECT BY level <= REGEXP_COUNT( t.input_name, '([^,]+)(,|$)' )
             ) AS pair_table
           )
         ) v
)
GROUP BY rn

<强> Results

|   A |   B |   C |   D |      E |      F |      G |     A1 |     B1 |     C1 |     D1 |
|-----|-----|-----|-----|--------|--------|--------|--------|--------|--------|--------|
|   1 |   2 |   3 |   4 |      5 |      6 |      7 |     11 |     12 |     13 |     14 |
| 400 | 300 | 200 | 100 | (null) | (null) | (null) | (null) | (null) | (null) | (null) |

您还可以使用PIVOT语句而不是多个MAX( CASE ... END )语句。

如果您没有静态输入名称集,则需要搜索dynamic pivots

<强>更新

  

有没有办法可以避免使用类型?

您可以避免创建类型,只使用内置的VARRAY或像SYS.ODCIVARCHAR2LIST这样的集合,但是您需要两个列表,两者之间的关联变得复杂。

WITH input_names ( rid, idx, name ) AS (
  SELECT t.ROWID,
         ROW_NUMBER() OVER ( PARTITION BY t.ROWID ORDER BY ROWNUM ) AS rn,
         v.COLUMN_VALUE
  FROM   t1 t
         CROSS JOIN
         TABLE(
           CAST(
             MULTISET(
               SELECT REGEXP_SUBSTR( t.input_name,   '([^,]+)(,|$)', 1, LEVEL, NULL, 1 )
               FROM   DUAL
               CONNECT BY level <= REGEXP_COUNT( t.input_name, '([^,]+)(,|$)' )
             ) AS SYS.ODCIVARCHAR2LIST
           )
         ) v
),
input_values ( rid, idx, value ) AS (
  SELECT t.ROWID,
         ROW_NUMBER() OVER ( PARTITION BY t.ROWID ORDER BY ROWNUM ) AS rn,
         v.COLUMN_VALUE
  FROM   t1 t
         CROSS JOIN
         TABLE(
           CAST(
             MULTISET(
               SELECT REGEXP_SUBSTR( t.input_values,   '([^,]+)(,|$)', 1, LEVEL, NULL, 1 )
               FROM   DUAL
               CONNECT BY level <= REGEXP_COUNT( t.input_values, '([^,]+)(,|$)' )
             ) AS SYS.ODCIVARCHAR2LIST
           )
         ) v
),
correlated ( name, value, rn ) AS (
  SELECT n.name,
         v.value,
         ROW_NUMBER() OVER ( PARTITION BY n.name
                             ORDER BY ROWNUM )
  FROM   input_names n
         INNER JOIN
         input_values v
         ON ( n.rid = v.rid AND n.idx = v.idx )
)
SELECT MAX( CASE name WHEN 'a' THEN value END ) AS a,
       MAX( CASE name WHEN 'b' THEN value END ) AS b,
       MAX( CASE name WHEN 'c' THEN value END ) AS c,
       MAX( CASE name WHEN 'd' THEN value END ) AS d,
       MAX( CASE name WHEN 'e' THEN value END ) AS e,
       MAX( CASE name WHEN 'f' THEN value END ) AS f,
       MAX( CASE name WHEN 'g' THEN value END ) AS g,
       MAX( CASE name WHEN 'a1' THEN value END ) AS a1,
       MAX( CASE name WHEN 'b1' THEN value END ) AS b1,
       MAX( CASE name WHEN 'c1' THEN value END ) AS c1,
       MAX( CASE name WHEN 'd1' THEN value END ) AS d1
FROM   correlated
GROUP BY rn;