2017-02-18 54 views
0

我需要在連接內循環,這就是我認爲我寫的。 我發佈了代碼。拆分正則表達式並循環遍歷

select listagg(request_num,',') within group (order by request_num) as request_num,segmentation_name from (
select MST.REQUEST_NUM,seg_dtls.SEGMENT_NAME,LAST_UPDATED_date,seg_dtls.segmentation_name from 
(select * from rp_sr_master) Mst, 
(select SUBSTR(ANSWER,1,INSTR (ANSWER, '~', 1)-1) AS SM_ID,sr_id from rp_sR_details 
WHERE Q_ID in (SELECT Q_ID FROM RP_QUESTIONS WHERE field_id='LM_LRE_Q6') 
    ) Dtls, (select SM_ID, SQL_STATEMENT, CREATION_DATE, UPDATED_DATE, SEGMENT_NAME,segmentation_name ,TOTAL_COUNT 
from rp_sEGMENT_master) seg_dtls 
where Dtls.SM_ID=seg_dtls.SM_ID 
and Dtls.sr_id=Mst.sr_id) 
group by segmentation_name; 

我這裏面臨的問題是在下面,

(select SUBSTR(ANSWER,1,INSTR (ANSWER, '~', 1)-1) AS SM_ID,sr_id from rp_sR_details 
    WHERE Q_ID in (SELECT Q_ID FROM RP_QUESTIONS WHERE field_id='LM_LRE_Q6') 
     ) 

在上面的代碼中,答案是這樣的:

2603~NG non IaaS IT Professional^2600~NG non IaaS Senior IT^2598~NG data profiling SENIOR IT professional^2595~Nigeria data profiling IT professiona

只挑選的第一個數字這是2603和其他人將被排除在外。

有沒有什麼辦法可以循環回答'ANSWER'中的所有數字。 我正在尋找想法。

謝謝。

回答

1

一個想法是使用方法分裂逗號分隔的字符串爲行,
你可以找到這種方法的例子在以下答案:

Splitting comma separated values in Oracle

How can I use regex to split a string, using a string as a delimiter?

上述解決方案使用regexp_substr函數。
如果您深入瞭解Oracle的REGEXP_SUBSTR function的詳細信息,那麼您將發現有可選的position參數。
SQL to generate a list of numbers from 1 to 100
(即SELECT LEVEL n FROM DUAL CONNECT BY LEVEL <= 100)在下面的方式:

with xx as (
select '2603~NG non IaaS IT Professional^2600~NG non IaaS Senior ' 
     || 'IT^2598~NG data profiling SENIOR IT professional^2595~Nigeria ' 
     || 'data profiling IT professiona' as answer 
from dual 
) 
select LEVEL AS n, regexp_substr(answer, '\d+', 1, level) as nbr 
from xx 
connect by level <= 6 
; 

上面的查詢產生以下結果:

此參數可與在此答案所示的sulution組合

N |NBR | 
--|-----| 
1 |2603 | 
2 |2600 | 
3 |2598 | 
4 |2595 | 
5 |  | 
6 |  | 

我們需要的是消除res中的空值ultset,它可以使用一個簡單的條件IS NOT NULL

with xx as (
select '2603~NG non IaaS IT Professional^2600~NG non IaaS Senior ' 
     || 'IT^2598~NG data profiling SENIOR IT professional^2595~Nigeria ' 
     || 'data profiling IT professiona' as answer 
from dual 
) 
select LEVEL AS n, regexp_substr(answer, '\d+', 1, level) as nbr 
from xx 
connect by regexp_substr(answer, '\d+', 1, level) IS NOT NULL 
; 

N |NBR | 
--|-----| 
1 |2603 | 
2 |2600 | 
3 |2598 | 
4 |2595 | 

上面的查詢工作完美的一個記錄來完成,但它會很困惑,當我們試圖解析2點或更多的行。幸運的是SO另一個答案,幫助解決這個問題:

ID |N |NBR | 
---|--|------| 
1 |1 |2603 | 
1 |2 |2600 | 
1 |3 |2598 | 
1 |4 |2595 | 
2 |1 |11111 | 
2 |2 |22222 | 
2 |3 |2598 | 
2 |4 |33333 | 
2 |5 |44 | 
2 |6 |5  | 
2 |7 |66 | 

Is there any alternative for OUTER APPLY in Oracle?


-- source data 
WITH xx as (
select 1 AS id, 
     '2603~NG non IaaS IT Professional^2600~NG non IaaS Senior ' 
     || 'IT^2598~NG data profiling SENIOR IT professional^2595~Nigeria ' 
     || 'data profiling IT professiona' as answer 
from dual 
UNION ALL 
select 2 AS id, 
     '11111~NG non IaaS IT Professional^22222~NG non IaaS Senior ' 
     || 'IT^2598~NG data 33333 profiling SENIOR IT professional^44~Nigeria ' 
     || 'data profiling 5 IT professiona 66' as answer 
from dual 
) 
-- end of source data 


SELECT t.ID, t1.n, t1.nbr 
FROM xx t 
CROSS JOIN LATERAL (
     select LEVEL AS n, regexp_substr(t.answer, '\d+', 1, level) as nbr 
     from dual 
     connect by regexp_substr(t.answer, '\d+', 1, level) IS NOT NULL 
) t1; 

上面的查詢從兩個記錄和顯示然後在下面的表格解析數


我相信你會設法將這個簡單的「解析」查詢合併到你的main查詢。

+0

謝謝,我是一個試圖學習。所以這裏是我更多的疑惑。 '\ d +'查找表達式中的數字。我處於345〜nas 2016^234〜ras2034^34_234^help 3的回答狀態。這裏我嘗試了一個regsub來查找^和〜之間的數字,但第一個數字不會有^。我試過這個[0-9] *(?=〜)。 – user3165555