[英]Fuzzy match on a left join
I am looking to join two tables where there's a 90% match for example.例如,我希望加入两个匹配率为 90% 的表。
Taking the example below, I want to join table A and table B on the phone number.以下面的示例为例,我想在电话号码上加入表 A 和表 B。 You can see that the phone numbers differ slightly (the international code).您可以看到电话号码略有不同(国际代码)。 I'd like the end result to show as table C.我希望最终结果显示为表 C。
I imagine it'll be something like but the join would specify to match on 90% of the phone_number
我想它会是这样的,但连接会指定匹配 90% 的phone_number
select
a.*,
b.most_recent_booking_date
from a
left join b
on a.phone_number = b.phone_number
Hope that's clear and any help would be great!希望这很清楚,任何帮助都会很棒! Cheers!干杯!
Table A表 A
Phone number电话号码 | Most recent call date最近通话日期 |
---|---|
441234567891 441234567891 | 01/05/22 22 年 1 月 5 日 |
441234567892 441234567892 | 02/05/22 02/05/22 |
Table B表 B
Phone number电话号码 | Most recent booking date最近的预订日期 |
---|---|
+441234567891 +441234567891 | 03/05/22 22 年 3 月 5 日 |
+441234567892 +441234567892 | 04/05/22 22 年 4 月 5 日 |
Table C表 C
Phone number电话号码 | Most recent call date最近通话日期 | Most recent bookingdate最近的预订日期 |
---|---|---|
441234567891 441234567891 | 01/05/22 22 年 1 月 5 日 | 03/05/22 22 年 3 月 5 日 |
441234567892 441234567892 | 02/05/22 02/05/22 | 04/05/22 22 年 4 月 5 日 |
You can try something like this, but I don't like it, as Demeteor says you should have an ID to join on.你可以尝试这样的事情,但我不喜欢它,因为 Demeteor 说你应该有一个 ID 才能加入。 Note that I use a left join here, in case there is no data in table #T2.请注意,我在这里使用左连接,以防表#T2 中没有数据。 I was also considering a computed column where it removes the + and then you could join that way too.我还在考虑一个计算列,它会删除 +,然后你也可以加入这种方式。 I will also get told off for SQL injection if the phone number could be dodgy.如果电话号码可能是狡猾的,我也会被告知 SQL 注入。
CREATE TABLE #T1 (
PhoneNumber VARCHAR(20) NOT NULL,
CallDate DATE NOT NULL
);
CREATE TABLE #T2 (
PhoneNumber VARCHAR(20) NOT NULL,
BookingDate DATE NOT NULL
);
INSERT INTO #T1 (PhoneNumber, CallDate)
VALUES
('441234567891', '20220501'),
('441234567892', '20220502');
INSERT INTO #T2 (PhoneNumber, BookingDate)
VALUES
('+441234567891', '20220503'),
('+441234567892', '20220504');
GO
SELECT *
FROM #T1 AS T1
LEFT JOIN #T2 AS T2 ON T2.PhoneNumber LIKE '%' + T1.PhoneNumber;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.