簡體   English   中英

SQL查詢僅返回5條記錄而不是6行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM