在Oracle sql中选择分隔的字符串作为表

时间:2016-06-21 04:52:32

标签: sql oracle oracle11g

我有一个字符串:

  

“宽度:10 | 7 | 20 | 45,身高:25 | 5 | 6 | 45,长度:35 | 6 | 3 | 4”

我正在寻找一个选择查询来选择它作为像这样的表:

Width | Height | Length
-----------------------
10    |  25    |   35  
7     |  5     |   6   
20    |  6     |   3  
45    |  45    |   4  

如果您需要更多信息,请发表评论。

4 个答案:

答案 0 :(得分:3)

此解决方案适用于任意数量的列(宽度,高度,...)和值。

-- your test data  
with data(val) as
 (select 'Width:10|7|20|45,Height:25|5|6|45,Length:35|6|3|4' from dual),

-- split by ,
cols as
 (select regexp_substr(str, '[^,]+', 1, level) val
    from (select val as str from data)
  connect by regexp_substr((select val as str from data),
                           '[^,]+',
                           1,
                           level) is not null),

-- split by :
hdr_and_cols as
 (select substr(val, 1, instr(val, ':') - 1) as hdr,
         substr(val, instr(val, ':') + 1) as val
    from cols),

-- split by |
hdr_lvl_vals as
 (select distinct x.hdr,
                  level as entry,
                  regexp_substr(x.val, '[^|]+', 1, level) as val
    from hdr_and_cols x
  connect by regexp_substr(x.val, '[^|]+', 1, level) is not null)

select * from hdr_lvl_vals;

结果:

hdr     entry   value
---------------------
Height  1       25
Height  2       5
Height  3       6
Height  4       45
Length  1       35
Length  2       6
Length  3       3
Length  4       4
Width   1       10
Width   2       7
Width   3       20
Width   4       45

您可以按照自己喜欢的方式格式化结果,例如

-- your test data  
with data(val) as
 (select 'Width:10|7|20|45,Height:25|5|6|45,Length:35|6|3|4' from dual),

-- split by ,
cols as
 (select regexp_substr(str, '[^,]+', 1, level) val
    from (select val as str from data)
  connect by regexp_substr((select val as str from data),
                           '[^,]+',
                           1,
                           level) is not null),

-- split by :
hdr_and_cols as
 (select substr(val, 1, instr(val, ':') - 1) as hdr,
         substr(val, instr(val, ':') + 1) as val
    from cols),

-- split by |
hdr_lvl_vals as
 (select distinct x.hdr,
                  level as entry,
                  regexp_substr(x.val, '[^|]+', 1, level) as val
    from hdr_and_cols x
  connect by regexp_substr(x.val, '[^|]+', 1, level) is not null)

-- format output
select w.val as width, h.val as heigth, l.val as length
  from (select entry, val from hdr_lvl_vals where hdr = 'Width') w,
       (select entry, val from hdr_lvl_vals where hdr = 'Height') h,
       (select entry, val from hdr_lvl_vals where hdr = 'Length') l,
       (select level as entry
          from dual
        connect by level <= (select max(entry) from hdr_lvl_vals)) r
 where r.entry = w.entry
   and r.entry = h.entry
   and r.entry = l.entry;

输出:

WIDTH   HEIGTH  LENGTH
--------------------
10      25      35
7       5       6
20      6       3
45      45      4

答案 1 :(得分:2)

你是说这个意思吗?在你的oracle中运行它:

IMediaControl::Pause

答案 2 :(得分:0)

您可以使用集合和数据透视表执行此操作:

Oracle安装程序

小助手功能:

CREATE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/

CREATE FUNCTION split_String(
  i_str    IN  VARCHAR2,
  i_delim  IN  VARCHAR2 DEFAULT ','
) RETURN stringlist DETERMINISTIC
AS
  p_result       stringlist := stringlist();
  p_start        NUMBER(5) := 1;
  p_end          NUMBER(5);
  c_len CONSTANT NUMBER(5) := LENGTH( i_str );
  c_ld  CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
  IF c_len > 0 THEN
    p_end := INSTR( i_str, i_delim, p_start );
    WHILE p_end > 0 LOOP
      p_result.EXTEND;
      p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
      p_start := p_end + c_ld;
      p_end := INSTR( i_str, i_delim, p_start );
    END LOOP;
    IF p_start <= c_len + 1 THEN
      p_result.EXTEND;
      p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
    END IF;
  END IF;
  RETURN p_result;
END;
/

<强>查询

WITH Data ( value ) AS (
  SELECT 'Width:10|7|20|45,Height:25|5|6|45,Length:35|6|3|4' FROM DUAL
)
SELECT *
FROM   (
  SELECT REGEXP_SUBSTR( c.COLUMN_VALUE, '^(.+?):(.+?)$', 1, 1, NULL, 1 ) AS name,
         TO_NUMBER( r.COLUMN_VALUE ) AS data,
         ROW_NUMBER() OVER ( PARTITION BY d.value, c.COLUMN_VALUE
                             ORDER BY ROWNUM
                           ) AS idx
  FROM   data d,
         TABLE( split_String( d.value, ',' ) ) c,
         TABLE( split_String(
           REGEXP_SUBSTR( c.COLUMN_VALUE, '^(.+?):(.+?)$', 1, 1, NULL, 2 ),
           '|'
         ) ) r
) PIVOT (
  MAX(data) FOR name IN ( 'Width' AS Width, 'Height' AS Height, 'Length' AS Length )
)
ORDER BY idx;

(注意:只需要一行指定列名称)

<强>输出

IDX WIDTH HEIGHT LENGTH
--- ----- ------ ------
  1    10     25     35
  2     7      5      6
  3    20      6      3
  4    45     45      4

答案 3 :(得分:0)

只是为了与ora:tokenize

一起玩
with w as (select 'Width:10|7|20|45,Height:25|5|6|45,Length:35|6|3|4' s from dual),
t(s) as (select /*+ NO_XML_QUERY_REWRITE  */  r from w,
       xmltable('ora:tokenize(.,",")'
                  passing s
            columns r varchar2(100) path '.')) 
select w.Width, w.Height, w.Length from(         
select /*+ NO_XML_QUERY_REWRITE  */  dig,rn,regexp_substr(s,'\w+') as gr from t,
       xmltable('
       for $c at $i in ora:tokenize(.,"\|")
      return <r><dig>{$c}</dig><rn>{$i}</rn></r>'
                  passing  regexp_replace(s,'\w+:')
                  columns 
            "DIG" varchar2(30) path '/r/dig',
            "RN" varchar2(30) path '/r/rn' 
            ))
      pivot
      ( 
      max(dig)
      for gr in('Width' as Width,'Height' as Height,'Length' as Length)
      )w
      ORDER BY RN