我已经开发了以下查询,但是无法正常工作:
WITH TABLE1 AS
(
SELECT 613414473 as ID, 1706014200964 as P_NUM, 119539 as d_id, 'F20.0' AS CODE FROM DUAL UNION ALL
SELECT 613414473 as ID, 1706014200964 as P_NUM, 119539 as d_id, 'F22.0' AS CODE FROM DUAL UNION ALL
SELECT 613415801 as ID, 1707045167741 as P_NUM, 115182 as d_id, 'A94.0' AS CODE FROM DUAL UNION ALL
SELECT 613415801 as ID, 1707045167741 as P_NUM, 115182 as d_id, NULL AS CODE FROM DUAL UNION ALL
SELECT 613417084 as ID, 1702038456441 as P_NUM, 6541 as d_id, 'E79' AS CODE FROM DUAL UNION ALL
SELECT 613417084 as ID, 1702038456421 as P_NUM, 6541 as d_id, 'I10' AS CODE FROM DUAL UNION ALL
SELECT 613418372 as ID, 1706226211517 as P_NUM, 25727 as d_id, 'F32.9' AS CODE FROM DUAL )
SELECT T1.*
, CASE when regexp_like( CODE, 'C0[5-9]|' ||
'A0[0-9]|A1[0-9]|A2[0-9]|A3[0-9]|A4[0-9]|A5[0-9]|A6[0-9]|A7[0-9]|A8[0-9]|A9[0-7]|' )
THEN 1
ELSE 0 END AS FOUND_CODE
FROM TABLE1 T1;
我希望将类似C0[5-9]%
或A0[0-97]
的代码标记为值1,然后如果找到至少一个设置所有标记的代码,则对同一p_num
进行标记p_num
到1。
以上示例输出:
| 613414473|1706014200964|119539|F20.0|0|
| 613414473|1706014200964|119539|F22.0|0|
| 613415801|1707045167741|115182|A94.0|1|
| 613415801|1707045167741|115182|NULL |1|
| 613417084|1702038456441|6541 |E79 |0|
| 613417084|1702038456421|6541 |I10 |0|
| 613418372|1706226211517|25727 |F32.9|0|
如何修改查询以获取该输出?还有我可以使用的更好的正则表达式吗?
答案 0 :(得分:1)
根据您的描述,正则表达式模式应为
'^(C0[5-9]|A[0-8][0-9]|A9[0-7])'
^
锚定到值的开头,并且括号允许任何用管道分隔的模式匹配;并简化了模式,因为A00至A89可以一键处理。
这标志着与原始查询相同的单行。下一步是将其移到子查询中,然后使用由p_num
分区的解析函数,您希望该函数很常见:
max(found_code) over (partition by p_num)
所以一起变成了(与其他行匹配另一个规则):
with table1 (id, p_num, d_id, code) as
(
select 613414470, 1706014200960, 119530, 'D99' from dual union all
select 613414471, 1706014200960, 119531, 'C05' from dual union all
--
select 613414473, 1706014200964, 119539, 'F20.0' from dual union all
select 613414473, 1706014200964, 119539, 'F22.0' from dual union all
select 613415801, 1707045167741, 115182, 'A94.0' from dual union all
select 613415801, 1707045167741, 115182, null from dual union all
select 613417084, 1702038456441, 6541 , 'E79' from dual union all
select 613417084, 1702038456421, 6541 , 'I10' from dual union all
select 613418372, 1706226211517, 25727 , 'F32.9' from dual
)
select id, p_num, d_id, code, max(found_code) over (partition by p_num) as found_code
from (
select t1.*
, case when regexp_like( code, '^(C0[5-9]|A[0-8][0-9]|A9[0-7])' )
then 1
else 0
end as found_code
from table1 t1
);
ID P_NUM D_ID CODE FOUND_CODE
------------- ------------- ------------- ----- -------------
613414470 1706014200960 119530 D99 1
613414471 1706014200960 119531 C05 1
613414473 1706014200964 119539 F20.0 0
613414473 1706014200964 119539 F22.0 0
613415801 1707045167741 115182 A94.0 1
613415801 1707045167741 115182 1
613417084 1702038456441 6541 E79 0
613417084 1702038456421 6541 I10 0
613418372 1706226211517 25727 F32.9 0