[英]SQL Query Returning only 5 records instead of 6 rows
我试图获取用双哈希(##)分隔的单列中存在的数据。 根据下面提到的查询,我只能提取5条记录,而不是6行。
我可以认为connectby
表达式存在一些问题。 任何帮助是极大的赞赏。
数据
Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##
查询用于获取单个记录中的记录,这些记录用双井号##分隔
复制方案:
create table temp (errormessage varchar2(300))
insert into temp errormessage values('Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##')
WITH sample_data
AS ( SELECT errormessage AS Error_Message
FROM TEMP )
SELECT Regexp_substr( error_message, ',?([^#]*)', 1, LEVEL, 'i', 1 ) AS Error_Message
FROM sample_data
WHERE Length( Regexp_substr( error_message, ',?([^#]*)', 1, LEVEL, 'i', 1 ) ) != 0
CONNECT BY ( Regexp_count(error_message, '#') + 1 >= LEVEL AND
PRIOR dbms_random.value IS NOT NULL )
ORDER BY LEVEL
错误消息是具有要定界信息的列。 现在,很容易将问题复制到任何数据库中。
也许您正在追求类似:
WITH sample_data AS (SELECT 1 stagging_id,
'A' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##' error_message
FROM dual UNION ALL
SELECT 2 stagging_id,
'B' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##Line7 ##' error_message
FROM dual)
SELECT stagging_id,
status,
regexp_substr(error_message, '[^#]+', 1, LEVEL) err_msg
FROM sample_data
CONNECT BY PRIOR stagging_id = stagging_id
AND PRIOR sys_guid() IS NOT NULL
AND regexp_substr(error_message, '[^#]+', 1, LEVEL) IS NOT NULL;
STAGGING_ID STATUS ERR_MSG
----------- ------ --------------------------------------------------------------
1 A Line1
1 A Line2
1 A Line3
1 A Line4
1 A Line5
1 A Line6
2 B Line1
2 B Line2
2 B Line3
2 B Line4
2 B Line5
2 B Line6
2 B Line7
现有代码的问题是regexp_substr中的*
,加上您正在计算单个#
而分隔符为##
的事实。
您可以这样修改查询:
WITH sample_data AS (SELECT 1 stagging_id,
'A' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##' error_message
FROM dual UNION ALL
SELECT 2 stagging_id,
'B' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##Line7 ##' error_message
FROM dual)
SELECT Regexp_substr( error_message, ',?([^#]+)', 1, LEVEL, 'i', 1 ) AS Error_Message
FROM sample_dataCONNECT BY ( Regexp_count(error_message, '##') >= LEVEL AND
PRIOR stagging_id = stagging_id AND
PRIOR dbms_random.value IS NOT NULL )
ORDER BY stagging_id, LEVEL;
ERROR_MESSAGE
--------------------------------------------------------------
Line1
Line2
Line3
Line4
Line5
Line6
Line1
Line2
Line3
Line4
Line5
Line6
Line7
请注意,我如何将regexp_substr中的*
s更改为+
s,将regexp_count中的'#'
更改为'##'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.