在 SQL 中使用交叉应用时查找两个表之间的匹配值

Find matching values between two tables when using cross apply in SQL

我正在尝试从 Table 列 x 中提取 5 个或更多连续数字,并将提取的数字与 [=54= 匹配] B列z。如果该值存在,那很好,但如果该值不存在,则需要将其插入 Table B。我设法从 Table A 中提取了数字,但是在尝试 JOIN 时我卡住了,因为我使用的是 CROSS APPLYCASE.也许我只是不明白该怎么做。

我用来提取数字的代码:

SELECT nvt.AdditionalInformation,
       CASE
           WHEN M.FirstMatch > 0
           THEN SUBSTRING(AdditionalInformation, M.FirstMatch-1, N.SecondMatch-1)
          
           --check IF TPNumber EXISTS in ChangeRequests table then add ChangeRequest Id to ReportVersionChangeRequests table
           ELSE NULL
       END
FROM
(
    SELECT ':'+nvt.AdditionalInformation+':' AdditionalInformation
    FROM dbo.NSReportVtest nvt
) nvt

CROSS APPLY(VALUES(PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', ':'+nvt.AdditionalInformation+':'))) M(FirstMatch)
CROSS APPLY(VALUES(PATINDEX('%[^0-9]%', SUBSTRING(AdditionalInformation, M.FirstMatch-1, LEN(AdditionalInformation))))) N(SecondMatch)

目前这是代码的结果:

Table答:

AdditionalInformation (No Column name)
:Test Results: NULL
:Test Results: 256985
:Test Results: NULL
:Test Results: NULL
:Test Results: NULL
:Test Results: 85965

预期结果:

Table答:

AdditionalInformation (No Column name)
:Test Results: NULL
:Test Results: 256985
:Test Results: NULL
:Test Results: NULL
:Test Results: NULL
:Test Results: 85965

Table乙:

Id Number
1 61758
2 85965
3 56456
4 78945

Join

后的预期输出

Table C:

Id Number
1 61758
2 85965
6 56456
8 78945
9 256985 (Added entry)
create table NSReportVtest (AdditionalInformation nvarchar(max)) -- Source table
create table NSReportVtest2 (id int identity(1,1) ,Value int) -- destination table


insert into NSReportVtest  --- creating source data 
select 'aaa256985bbb'
union all select 'aasa123456babb'
union all select 'aaga245bfbb'
union all select 'abaa54123bnbb'
union all select 'aaba654987bmbb'
union all select 'aacabybb'


insert into NSReportVtest2(Value) -- creating dest data 
select 123456
union all select 54123 


insert into NSReportVtest2  --- inserting missing data using left join 
select cast( substring(SUBSTRING(AdditionalInformation,PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', AdditionalInformation), LEN(AdditionalInformation)) ,1,PATINDEX('%[^0-9]%',SUBSTRING(AdditionalInformation,PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', AdditionalInformation), LEN(AdditionalInformation))) -1 ) as int) Value
 from  NSReportVtest t1
left outer join  NSReportVtest2 t2
on cast( substring(SUBSTRING(AdditionalInformation,PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', AdditionalInformation), LEN(AdditionalInformation)) ,1,PATINDEX('%[^0-9]%',SUBSTRING(AdditionalInformation,PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', AdditionalInformation), LEN(AdditionalInformation))) -1 ) as int) = t2.Value
where t2.id is null 
and  cast( substring(SUBSTRING(AdditionalInformation,PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', AdditionalInformation), LEN(AdditionalInformation)) ,1,PATINDEX('%[^0-9]%',SUBSTRING(AdditionalInformation,PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', AdditionalInformation), LEN(AdditionalInformation))) -1 ) as int)  <>0 

我找到了一个有效的解决方案。我 INSERT INTO 一个 #temp table 然后我可以 select 特定的列并将其连接到第二个 table 这样可以更容易地管理数据。

SELECT nv.AdditionalInformation, 
       nv.Id,
       CASE
           WHEN M.FirstMatch > 0
           THEN SUBSTRING(AdditionalInformation, M.FirstMatch-1, N.SecondMatch-1)
           ELSE NULL
       END AS ExtractedTP
INTO #temp
FROM
(
    SELECT ':'+nv.AdditionalInformation+':' AdditionalInformation, 
           nv.Id
    FROM dbo.NSReportVtest nv
) nv
CROSS APPLY(VALUES(PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', ':'+nv.AdditionalInformation+':'))) M(FirstMatch)
CROSS APPLY(VALUES(PATINDEX('%[^0-9]%', SUBSTRING(AdditionalInformation, M.FirstMatch-1, LEN(AdditionalInformation))))) N(SecondMatch);

--select and join temp table to ChangeRequests table to see which TP Numbers exist

SELECT t.Id, 
       t.ExtractedTP, 
       nrt.TPNumber, 
       nrt.Id
FROM #temp t
     LEFT JOIN dbo.NSChangeRequestsTest nrt ON t.ExtractedTP = nrt.TPNumber
ORDER BY t.ExtractedTP DESC;