【问题标题】:Using a mapping table with multiple values per attribute使用每个属性具有多个值的映射表
【发布时间】:2014-03-27 10:01:54
【问题描述】:

我收到了一个表 t_mapping,其中的值显示在 列中,例如 [1..5]、[8..12]、15,16 或 17,18 或 20 或 [21..45] prd_product_nl。我必须在 prd_product_nl 中找到我的值以查找 ma​​pping_result(格式:结果 a,或结果 b 或结果 c 或结果 d)

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

作为一种复杂情况,[1..5] 表示将值 1 向上描述,包括 5。

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

【问题讨论】:

  • 您的示例数据没有意义。我建议你提供一个 SQLFiddle,或者至少在这里格式化它。

标签: sql oracle select oracle11g


【解决方案1】:

您可以通过分层查询来实现:

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

【讨论】:

    猜你喜欢
    • 2015-08-12
    • 2020-10-05
    • 1970-01-01
    • 2011-03-15
    • 2021-09-07
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2020-06-25
    相关资源
    最近更新 更多