1

Oracle 10g、次の列があるとします。

col
------------------------------------------------------------------------------------------------
[1,98]([1,81]([6,100828],[6,101260]),[1,81]([6,100529],[6,101259]),[1,81]([6,101709],[6,100474]))

そして、私はこの結果を表示したい:

col
------
100828
101260
100529
101259
101709
100474

この結果を SQL クエリで表示することはできますか?

実際に私が試したこと:

SELECT SUBSTR(col, INSTR(col, ',', 1, 3) + 1, 6) exp_1,
       SUBSTR(col, INSTR(col, ',', 1, 5) + 1, 6) exp_2,
       SUBSTR(col, INSTR(col, ',', 1, 8) + 1, 6) exp_3,
       SUBSTR(col, INSTR(col, ',', 1, 10) + 1, 6) exp_4,
       SUBSTR(col, INSTR(col, ',', 1, 13) + 1, 6) exp_5,
       SUBSTR(col, INSTR(col, ',', 1, 15) + 1, 6) exp_6
  FROM (SELECT '[1,98]([1,81]([6,100828],[6,101260]),[1,81]([6,100529],[6,101259]),[1,81]([6,101709],[6,100474]))' col
          FROM dual) ; 

EXP_1  EXP_2  EXP_3  EXP_4  EXP_5  EXP_6
------ ------ ------ ------ ------ ------
100828 101260 100529 101259 101709 100474

ただし、返される exp_% の数は可変であり、常にペアである可能性があります。つまり、別の行が 8 exp_% を返す可能性があります。

SUBSTR(col, INSTR(col, ',', 1, 18) + 1, 6) exp_7 ,
SUBSTR(col, INSTR(col, ',', 1, 20) + 1, 6) exp_8

exp_% の数が修正された場合の提案も大歓迎です!

ありがとう。

4

1 に答える 1

2

テーブルの名前が「foo」で、列名が「col」であるとします。

with q as (
    select ','||regexp_replace(
        regexp_replace(
            regexp_replace(
                regexp_replace(col, '[[0-9,]*]\(', ''), 
                    '\[[0-9],', ''), 
                '[])]', ','), 
        ',,+', 
        ',') a from foo
) 
select data 
  from (select substr(a, instr(a, ',', 1, rownum) + 1, 6) data 
          from q,
               (select 1 from q connect by level < length(regexp_replace(a, '[0-9]', '')))
       )
;

これが説明です。これはすぐに複雑になり、おそらくうまく拡張できないので、購入者は注意してください。

まず、「[1,98](」フィールドを取り除きたいと思います。

  1  with q as (
  2  select
  3          regexp_replace(col, '[[0-9,]*]\(', '')
  4  from foo
  5  )
  6* select * from q

REGEXP_REPLACE(COL,'[[0-9,]*]\(','')
------------------------------------------------------------------------------------------------------------------------------------
[6,100828],[6,101260])[6,100529],[6,101259])[6,101709],[6,100474]))

次に、フィールドの '[n,' 部分を取り除きたいと思います。

  1  with q as (
  2  select
  3      regexp_replace(
  4          regexp_replace(col, '[[0-9,]*]\(', ''),
  5          '\[[0-9],', ''
  6      ) a from foo
  7  )
  8* select * from q

A
------------------------------------------------------------------------------------------------------------------------------------
100828],101260])100529],101259])101709],100474]))

すべての ']' と ')' を削除します。

  1  with q as (
  2  select
  3      regexp_replace(
  4      regexp_replace(
  5          regexp_replace(col, '[[0-9,]*]\(', ''),
  6          '\[[0-9],', ''),
  7          '[])]', ',')
  8       a from foo
  9  )
 10* select * from q

A
------------------------------------------------------------------------------------------------------------------------------------
100828,,101260,,100529,,101259,,101709,,100474,,,

重複するコンマを取り除き、先頭にコンマを追加します。

  1  with q as (
  2  select ','||regexp_replace(
  3      regexp_replace(
  4      regexp_replace(
  5          regexp_replace(col, '[[0-9,]*]\(', ''),
  6          '\[[0-9],', ''),
  7          '[])]', ','),
  8      ',,+',
  9      ',') a from foo
 10  )
 11* select * from q

A
------------------------------------------------------------------------------------------------------------------------------------
,100828,101260,100529,101259,101709,100474,

それらがいくつのフィールドであるかを把握し、それぞれの行を作成します。

  1  with q as (
  2  select ','||regexp_replace(
  3      regexp_replace(
  4      regexp_replace(
  5          regexp_replace(col, '[[0-9,]*]\(', ''),
  6          '\[[0-9],', ''),
  7          '[])]', ','),
  8      ',,+',
  9      ',') a from foo
 10  )
 11* select 1 from q connect by level < length(regexp_replace(a, '[0-9]', ''))

     1
----------
     1
     1
     1
     1
     1
     1
     1

q (テーブルに複数の行がある場合、これは機能しないことに注意してください) と部分文字列を使用してデカルト結合を実行し、最終的な答えを取得します。

  1  with q as (
  2  select ','||regexp_replace(
  3      regexp_replace(
  4      regexp_replace(
  5          regexp_replace(col, '[[0-9,]*]\(', ''),
  6          '\[[0-9],', ''),
  7          '[])]', ','),
  8      ',,+',
  9      ',') a from foo
 10  )
 11  select data
 12    from (select substr(a, instr(a, ',', 1, rownum) + 1, 6) data
 13        from q,
 14         (select 1 from q connect by level < length(regexp_replace(a, '[0-9]', '')))
 15*        )

DATA
------
100828
101260
100529
101259
101709
100474


6 rows selected.
于 2011-10-18T13:30:06.843 に答える