分割字串在Oracle SQL中无法正常运作

时间:2019-03-20 17:25:53

标签: sql oracle split

我的桌子是:

file_content
MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
OBR|1|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN
OBR|2|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN

我有以下SQL查询不适用于上述情况:

SELECT c.COLUMN_VALUE AS obr_seq, REGEXP_SUBSTR( t.file_content, '^MSH.*?' || CHR(10) || 'PID.*?' || CHR(10) )
         || REGEXP_SUBSTR( t.file_content, 'OBR.*?' || CHR(10) || '((OBX|NTE|FT1).*?($|' || CHR(10) || '))*', 1, c.COLUMN_VALUE ) AS split_value
FROM   test_hl7_message_split t
       LEFT OUTER JOIN
       TABLE(
         CAST(
           MULTISET(
             SELECT LEVEL
             FROM   DUAL
             CONNECT BY LEVEL <= REGEXP_COUNT( t.file_content, 'OBR.*?' || CHR(10) || '((OBX|NTE|FT1).*?($|' || CHR(10) || '))*' )
           ) AS SYS.ODCINUMBERLIST
         )
       ) c
       ON ( 1 = 1 );

这给了我错误的输出:

OBR_SEQ     SPLIT_VALUE
1           MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
            PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
            OBRIEN|01572633|20923085580^TESTPAT||19730204|

2           MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
            PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
            OBR|1|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
            OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
            OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN

3           MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
            PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
            OBR|2|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
            OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
            OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN

正确的输出应该是:

OBR_SEQ     SPLIT_VALUE
1           MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
            PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
            OBR|1|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
            OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
            OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN

2           MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
            PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
            OBR|2|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
            OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
            OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN

基本上,我的脚本正在比较段中间的OBR,但我不想要它。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

其与标题OBR中的OBRIEN匹配。只需对字符串进行预处理以将标头与正文分开,然后可以仅对消息正文进行迭代:

Oracle设置

CREATE TABLE test_hl7_message_split ( file_content ) AS
SELECT 'MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2
PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|
OBR|1|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN
OBR|2|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F
OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X
OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN' FROM DUAL;

查询

SELECT c.COLUMN_VALUE AS obr_seq,
       head
         || REGEXP_SUBSTR( t.body, 'OBR.*?' || CHR(10) || '((OBX|NTE|FT1).*?($|' || CHR(10) || '))*', 1, c.COLUMN_VALUE ) AS split_value
FROM   ( SELECT REGEXP_SUBSTR( file_content, '^MSH.*?' || CHR(10) || 'PID.*?' || CHR(10) ) AS head,
                REGEXP_REPLACE( file_content, '^MSH.*?' || CHR(10) || 'PID.*?' || CHR(10) ) AS body
         FROM   test_hl7_message_split ) t
       LEFT OUTER JOIN
       TABLE(
         CAST(
           MULTISET(
             SELECT LEVEL
             FROM   DUAL
             CONNECT BY LEVEL <= REGEXP_COUNT( t.body, 'OBR.*?' || CHR(10) || '((OBX|NTE|FT1).*?($|' || CHR(10) || '))*' )
           ) AS SYS.ODCINUMBERLIST
         )
       ) c
       ON ( 1 = 1 );

输出

OBR_SEQ | SPLIT_VALUE                                                                                                                                                                                                                                                                                                                                                                                                                    
------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      1 | MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2<br>PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|<br>OBR|1|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F<br>OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X<br>OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN<br>
      2 | MSH|^~\&|LCS|LCA|LIS|TEST9999|199807311532||ORU^R01|3630|P|2.2<br>PID|3|2161348473|OBRIEN|01572633|20923085580^TESTPAT||19730204|<br>OBR|2|8642753100013^LIS|20923085580^LCS|083824^PANEL 083824^L|||19980728083600|||||| CH13380|19980728000000||||||20923085580||19980730041800|||F<br>OBX|1|NM|150001^HIV-1 ABS-O.D. RATIO^L|||||||N|X<br>OBX|2|CE|001719^HIV-1 ABS, SEMI-QN^L||HTN|||||N|F|19910123|| 19980729155700|BN    

查询2

另一种选择是在正则表达式中使用m匹配参数将字符串视为多行,然后可以在^OBR上进行匹配,以仅在{该行:

OBR

db <>提琴https://supplier.com/Service