简体   繁体   English

T-SQL:比较2个未知值的列

[英]T-SQL: compare 2 columns with unknown values

I'm joining a table to itself in order to find people in my table in the same family with different last names. 我要将一个表自身连接起来,以便在同一家庭中的表中找到姓氏不同的人。 The only issue is there are instances where one last name might be Jones, and for another record the column might be Jones Jr. 唯一的问题是,在某些情况下,姓氏可能是Jones,对于另一条记录,该列可能是JonesJr。

These are technically the same last name so they don't fit my requirements. 这些在技术上是相同的姓氏,因此不符合我的要求。 I need to eliminate Jones Jr. from my results. 我需要从结果中消除小琼斯。

The complicating factor is it could also be something like Smith-Jones, so I'd need to remove this record too. 复杂的因素是,它也可能像Smith-Jones,所以我也需要删除该记录。 Since I don't know where the difference will be I would like to be able to add a condition to my query saying that no more than 4 (or some arbitrary number) characters of each name can match. 因为我不知道区别在哪里,所以我希望能够在查询中添加一个条件,说每个名称最多可以匹配4个(或任意数字)字符。

Here's my query: 这是我的查询:

SELECT [fields] 
FROM [table] a 
INNER JOIN [table] b ON a.[family_id] = b.[family_id]
WHERE a.[last_name] <> b.[last_name]

Any ideas? 有任何想法吗?

Use wildcards for the comparison. 使用通配符进行比较。 The following might work for what you want: 以下可能适用于您想要的:

SELECT [fields]
from [table] a INNER JOIN
     [table] b
     ON a.[family_id] = b.[family_id]
WHERE a.last_name not like '%' + b.last_name + '%' and
      b.last_name not like '%' + a.last_name + '%';

Of course, "Johns" and "Johnson" and "Martin" and "Martinez" will also fail to match. 当然,“ Johns”和“ Johnson”以及“ Martin”和“ Martinez”也将不匹配。 I don't know if that is an issue. 我不知道这是不是一个问题。

You could use DIFFERENCE. 您可以使用DIFFERENCE。 The value returned is the number of characters in the SOUNDEX values that are the same. 返回的值是SOUNDEX值中相同的字符数。 The return value ranges from 0 through 4: 0 indicates weak or no similarity, and 4 indicates strong similarity or the same values. 返回值的范围是0到4:0表示相似性很弱或没有相似性,而4则表示相似性很强或相同。 This would be a nice solution to your requirement: find people in my table in the same family with different last names 这将是满足您要求的一个很好的解决方案:在我的餐桌上的同一个家庭中找到姓氏不同的人

SELECT [fields] from [table] a 
INNER JOIN [table] b
ON a.[family_id] = b.[family_id]
WHERE DIFFERENCE(a.[last_name], b.[last_name]) < 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM