简体   繁体   English

SQL查询仅返回5条记录而不是6行

[英]SQL Query Returning only 5 records instead of 6 rows

I am trying to fetch the data present in a single column which is delimited with double hash (##). 我试图获取用双哈希(##)分隔的单列中存在的数据。 As per my query mentioned below, i am able to fetch only 5 records instead of 6 lines. 根据下面提到的查询,我只能提取5条记录,而不是6行。

I could think there is some issue with my connectby expression. 我可以认为connectby表达式存在一些问题。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Data 数据

Line1## Line2## Line3 ## Line4 ## Line5  ## Line6  ##

Query Used to fetch the records in a single record which are delimited with double hash ## 查询用于获取单个记录中的记录,这些记录用双井号##分隔

Replicate the scenario: 复制方案:

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 

Error Message is the column which has the info to be delimited. 错误消息是具有要定界信息的列。 Now it is pretty easy to replicate the issue in any of your databases. 现在,很容易将问题复制到任何数据库中。

Maybe you're after something like: 也许您正在追求类似:

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

The issue with your existing code is the * in the regexp_substr, plus the fact that you're counting single # whereas your delimiter is ## . 现有代码的问题是regexp_substr中的* ,加上您正在计算单个#而分隔符为##的事实。

You could fix your query like so: 您可以这样修改查询:

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

Note how I've changed the * s to + s in the regexp_substr's and the '#' to '##' in the regexp_count. 请注意,我如何将regexp_substr中的* s更改为+ s,将regexp_count中的'#'更改为'##'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM