【问题标题】:SQL Regex - Replace with substring from another fieldSQL Regex - 用另一个字段的子字符串替换
【发布时间】:2016-05-02 01:30:47
【问题描述】:

我有一个问卷反馈的数据库表(Oracle 11g),包括多项选择、多项回答问题。 Options 列包含用户可以选择的每个值,Answers 列包含他们选择的数值。

ID_NO     OPTIONS                               ANSWERS
1001      Apple Pie|Banana-Split|Cream Tea      1|2
1002      Apple Pie|Banana-Split|Cream Tea      2|3
1003      Apple Pie|Banana-Split|Cream Tea      1|2|3

我需要一个可以解码答案的查询,并将答案的文本版本作为单个字符串。

ID_NO     ANSWERS     ANSWER_DECODE
1001      1|2         Apple Pie|Banana-Split
1002      2|3         Banana-Split|Cream Tea
1003      1|2|3       Apple Pie|Banana-Split|Cream Tea

我已经尝试使用正则表达式来替换值和获取子字符串,但我无法找到正确合并两者的方法。

WITH feedback AS (
  SELECT 1001 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '1|2' answers FROM DUAL UNION
  SELECT 1002 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '2|3' answers FROM DUAL UNION
  SELECT 1003 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '1|2|3' answers FROM DUAL )
SELECT 
  id_no,
  options,
  REGEXP_SUBSTR(options||'|', '(.)+?\|', 1, 2) second_option,
  answers,
  REGEXP_REPLACE(answers, '(\d)+', ' \1 ') answer_numbers,
  REGEXP_REPLACE(answers, '(\d)+', REGEXP_SUBSTR(options||'|', '(.)+?\|', 1, To_Number('2'))) "???"
FROM feedback

我不想在 SQL 中手动定义或解码答案;有许多带有不同问题(以及不同数量的选项)的调查,所以我希望有一个解决方案可以动态地适用于所有这些问题。

我尝试将选项和答案按 LEVEL 拆分为单独的行,然后在代码匹配的地方重新加入它们,但这对于实际数据集运行速度非常慢(一个包含 600 行响应的 5 选项问题) .

WITH feedback AS (
  SELECT 1001 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '1|2' answers FROM DUAL UNION
  SELECT 1002 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '2|3' answers FROM DUAL UNION
  SELECT 1003 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '1|2|3' answers FROM DUAL )
SELECT
    answer_rows.id_no,
    ListAgg(option_rows.answer) WITHIN GROUP(ORDER BY option_rows.lvl)
FROM
  (SELECT DISTINCT
    LEVEL lvl,
    REGEXP_SUBSTR(options||'|', '(.)+?\|', 1, LEVEL) answer
  FROM
    (SELECT DISTINCT
      options,
      REGEXP_COUNT(options||'|', '(.)+?\|') num_choices
    FROM
      feedback)
  CONNECT BY LEVEL <= num_choices
  ) option_rows
  LEFT OUTER JOIN
  (SELECT DISTINCT
    id_no,
    to_number(REGEXP_SUBSTR(answers, '(\d)+', 1, LEVEL)) answer
  FROM
    (SELECT DISTINCT
      id_no,
      answers,
      To_Number(REGEXP_SUBSTR(answers, '(\d)+$')) max_answer
    FROM
      feedback)
  WHERE
    to_number(REGEXP_SUBSTR(answers, '(\d)+', 1, LEVEL)) IS NOT NULL
  CONNECT BY LEVEL <= max_answer
  ) answer_rows
    ON option_rows.lvl = answer_rows.answer
GROUP BY
    answer_rows.id_no
ORDER BY
  answer_rows.id_no

如果没有仅使用正则表达式的解决方案,是否有比 LEVEL 更有效的方法来拆分值?还是有其他可行的方法?

【问题讨论】:

标签: sql regex oracle regexp-replace regexp-substr


【解决方案1】:

这很慢,因为您将每一行展开太多次;您正在使用的 connect-by 子句正在查看所有行,因此您最终会得到大量数据然后进行排序 - 这大概就是您最终在其中得到 DISTINCT 的原因。

您可以将两个PRIOR 子句添加到connect-by,首先是保留ID_NO,其次是避免循环 - 任何非确定性函数都可以做到这一点,我选择了@987654324 @ 但如果您愿意,可以使用 sys_guid 或其他方式。你也不需要很多子查询,你可以用两个来完成;或者作为我认为更清晰的 CTE:

WITH feedback AS (
  SELECT 1001 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '1|2' answers FROM DUAL UNION
  SELECT 1002 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '2|3' answers FROM DUAL UNION
  SELECT 1003 id_no, 'Apple Pie|Banana-Split|Cream Tea' options, '1|2|3' answers FROM DUAL
),
option_rows AS (
  SELECT
    id_no,
    LEVEL answer,
    REGEXP_SUBSTR(options, '[^|]+', 1, LEVEL) answer_text
  FROM feedback
  CONNECT BY LEVEL <= REGEXP_COUNT(options, '[^|]+')
  AND id_no = PRIOR id_no
  AND PRIOR dbms_random.value IS NOT NULL
),
answer_rows AS (
  SELECT
    id_no,
    REGEXP_SUBSTR(answers, '[^|]+', 1, LEVEL) answer
  FROM feedback
  CONNECT BY LEVEL <= REGEXP_COUNT(answers, '[^|]+')
  AND PRIOR id_no = id_no
  AND PRIOR dbms_random.value IS NOT NULL
)
SELECT
  option_rows.id_no,
  LISTAGG(option_rows.answer, '|') WITHIN GROUP (ORDER BY option_rows.answer) AS answers,
  LISTAGG(option_rows.answer_text, '|') WITHIN GROUP (ORDER BY option_rows.answer) AS answer_decode
FROM option_rows
JOIN answer_rows
ON option_rows.id_no = answer_rows.id_no
AND option_rows.answer = answer_rows.answer
GROUP BY option_rows.id_no
ORDER BY option_rows.id_no;

得到:

     ID_NO ANSWERS    ANSWER_DECODE                          
---------- ---------- ----------------------------------------
      1001 1|2        Apple Pie|Banana-Split                  
      1002 2|3        Banana-Split|Cream Tea                  
      1003 1|2|3      Apple Pie|Banana-Split|Cream Tea  

我还更改了您的正则表达式模式,因此您不必附加或删除 |

【讨论】:

    【解决方案2】:

    看看这个紧凑的解决方案:

       with sample_data as
    (
      select 'ala|ma|kota' options, '1|2' answers from dual
      union all
      select 'apples|oranges|bacon', '1|2|3' from dual
      union all
      select 'a|b|c|d|e|f|h|i','1|3|4|5|8' from dual
    )
    select answers, options,
    regexp_replace(regexp_replace(options,'([^|]+)\|([^|]+)\|([^|]+)','\' || replace(answers,'|','|\')),'[|]+','|') answer_decode
    from sample_data;
    

    输出:

      ANSWERS   OPTIONS              ANSWER_DECODE
    --------- -------------------- ---------------------------
    1|2       ala|ma|kota          ala|ma
    1|2|3     apples|oranges|bacon apples|oranges|bacon
    1|3|4|5|8 a|b|c|d|e|f|h|i      a|c|d|f|h|i
    

    【讨论】:

    • 如果调查中有超过 3 个可能的答案会怎样?
    • Stil 只需少量额外清洁即可工作 - 去除不必要的分隔符
    【解决方案3】:

    我已经在 MySQL 中编写了一个封闭的解决方案(现在没有安装 Oracle) - 但我已经编写了需要更改的内容才能使查询在 Oracle 中工作。

    另外,我的代码中最丑的部分在 Oracle 中会更好看,因为它有更好的 INSTR 函数。

    想法是使用数字列表(1 到 10 以支持每个调查最多 10 个选项)进行 CROSS JOIN,并将 OPTIONS 字段分解为不同的行...(您可以通过同时使用数字列表和 Oracle 的 INSTR 函数,请参阅 cmets。

    从那里过滤掉未选择的行并将所有内容重新组合在一起。

    -- I've used GROUP_CONCAT in MySQL, but in Oracle you'll have to use WM_CONCAT
    select ID_NO, ANSWERS, group_concat(broken_down_options,'|') `OPTIONS`
    from (
        select your_table.ID_NO, your_table.ANSWERS, 
                -- Luckily, you're using ORACLE so you can use an INSTR function that has the "occurrence" parameter
                -- INSTR(string, substring, [position, [occurrence]])
                -- use the nums.num field as input for the occurrence parameter
                -- and just put '1' under "position"
                case when nums.num = 1 
                    then substr(your_table.`OPTIONS`, 1, instr(your_table.`OPTIONS`, '|') - 1)
                    when nums.num = 2
                    then substr(substr(your_table.`OPTIONS`, instr(your_table.`OPTIONS`, '|') + 1), 1, instr(substr(your_table.`OPTIONS`, instr(your_table.`OPTIONS`, '|') + 1), '|') - 1)
                    else substr(your_table.`OPTIONS`,  length(your_table.`OPTIONS`) - instr(reverse(your_table.`OPTIONS`), '|') + 2) end broken_down_options
        from (select 1 num union all
            select 2 num union all
            select 3 num union all
            select 4 num union all
            select 5 num union all
            select 6 num union all
            select 7 num union all
            select 8 num union all
            select 9 num union all
            select 10 num
            ) nums 
            CROSS JOIN
            (select 1001 ID_NO, 'Apple Pie|Banana-Split|Cream Tea' `OPTIONS`, '1|2' ANSWERS union
            select 1002 ID_NO, 'Apple Pie|Banana-Split|Cream Tea' `OPTIONS`, '2|3' ANSWERS union
            select 1003 ID_NO, 'Apple Pie|Banana-Split|Cream Tea' `OPTIONS`, '1|2|3' ANSWERS
            ) your_table
        -- for example: 2|3 matches 2 and 3 but not 1
        where your_table.ANSWERS like concat(concat('%',nums.num),'%')
    ) some_query
    group by ID_NO, ANSWERS
    

    【讨论】:

      【解决方案4】:

      创建一个存储的predure并执行以下步骤

      • 声明一个适合您大小的数组。
      • 从第一行获取option 数据。使用正则表达式或level 在管道之间提取值,然后将它们存储在数组中。注意:这将只是一次性的。所以你不需要为每一行重复它。
      • 现在在循环中,对于每一行,选择answers 并使用数组值来分配answers 的值

      【讨论】:

        猜你喜欢
        • 2011-04-06
        • 1970-01-01
        • 2011-06-06
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 2019-05-14
        • 2013-12-03
        • 1970-01-01
        相关资源
        最近更新 更多