使用每个属性具有多个值的映射表

时间:2014-03-27 10:01:54

标签: sql oracle select oracle11g

我已经递交了一个表格t_mapping,其值为[1..5],[8..12],15,16或17,18或20或[21..45],如列所示prd_product_nl 即可。我必须在 prd_product_nl 中找到我的值才能查找 mapping_result (格式:结果a,或结果b或结果c或结果d)

我想要查找的值是整数, prd_product_nl 中的值数组按升序排列,但格式如上所示。

作为一个复杂因素,[1..5]意味着描绘值1并包括5。

我可以编写一个查询,在一个存在多个不同数组格式值的列中查找整数吗?

1 个答案:

答案 0 :(得分:0)

您可以通过分层查询实现此目的:

with w1(list) as
(
  select '[1..5],[8..12],15,16' from dual
  union all
  select '17,18' from dual
  union all
  select '[21..45]' from dual
),
w2 as
(
  select distinct w1.list, regexp_substr(w1.list, '[^,]+', 1, level) part
  from w1
  connect by regexp_instr(w1.list, '[^,]+', 1, level) != 0
  order by w1.list
),
w3 as
(
  select w2.*,
         regexp_replace(w2.part, '\[(\d+)\.\.(\d+)]', '\1', 1) lower_bound,
         regexp_replace(w2.part, '\[(\d+)\.\.(\d+)]', '\2', 1) upper_bound
  from w2
)
select distinct w3.*, w3.lower_bound + level - 1 item
from w3
connect by level <= w3.upper_bound - w3.lower_bound + 1
order by 1, 5
;

这给出了:

LIST                    PART        LOWER_BOUND    UPPER_BOUND    ITEM
17,18                   17          17             17             17
17,18                   18          18             18             18
[1..5],[8..12],15,16    [1..5]      1              5              1
[1..5],[8..12],15,16    [1..5]      1              5              2
[1..5],[8..12],15,16    [1..5]      1              5              3
[1..5],[8..12],15,16    [1..5]      1              5              4
[1..5],[8..12],15,16    [1..5]      1              5              5
[1..5],[8..12],15,16    [8..12]     8              12             8
[1..5],[8..12],15,16    [8..12]     8              12             9
[1..5],[8..12],15,16    [8..12]     8              12             10
[1..5],[8..12],15,16    [8..12]     8              12             11
[1..5],[8..12],15,16    [8..12]     8              12             12
[1..5],[8..12],15,16    15          15             15             15
[1..5],[8..12],15,16    16          16             16             16
[21..45]                [21..45]    21             45             21
[21..45]                [21..45]    21             45             22
[21..45]                [21..45]    21             45             23
[21..45]                [21..45]    21             45             24
[21..45]                [21..45]    21             45             25
[21..45]                [21..45]    21             45             26
[21..45]                [21..45]    21             45             27
[21..45]                [21..45]    21             45             28
[21..45]                [21..45]    21             45             29
[21..45]                [21..45]    21             45             30
[21..45]                [21..45]    21             45             31
[21..45]                [21..45]    21             45             32
[21..45]                [21..45]    21             45             33
[21..45]                [21..45]    21             45             34
[21..45]                [21..45]    21             45             35
[21..45]                [21..45]    21             45             36
[21..45]                [21..45]    21             45             37
[21..45]                [21..45]    21             45             38
[21..45]                [21..45]    21             45             39
[21..45]                [21..45]    21             45             40
[21..45]                [21..45]    21             45             41
[21..45]                [21..45]    21             45             42
[21..45]                [21..45]    21             45             43
[21..45]                [21..45]    21             45             44
[21..45]                [21..45]    21             45             45