如何找到2列之间匹配的字符百分比

时间:2019-05-31 14:21:47

标签: sql sql-server tsql

我有下表

ID  ShopName            LocationName
1   GreatMall           Great,Mall
2   Gingermall          Gingermall
3   MARK.HI             MARK,HI
4   GALLERY INC         GALLERY. INC

如果列'ShopName' = 'LocationName',那么我想查找列值匹配的百分比。 (或找到理想的百分比来设置模糊匹配) 例如,在ID = 2表中,'ShopName''LocationName'列中的值匹配(Gingermall = Gingermall),则数据匹配的百分比为100%。 对于ID = 1,值不匹配(GreatMall <> Great,Mall),然后我想知道数据不匹配的百分比。

1 个答案:

答案 0 :(得分:0)

我尚未完全审查此解决方案,但它适用于您的数据样本:

设置

Create Table #tbl 
(
ID Int,
ShopName VarChar(25),
LocationName VarChar(25)
)
Insert Into #tbl Values
(1,'GreatMall','Great,Mall'), 
(2,'Gingermall','Gingermall'), 
(3,'MARK.HI','MARK,HI'), 
(4,'GALLERY INC','GALLERY. INC')

查询

Declare @results As Table (id Int, ShopName VarChar(25), LocationName VarChar(25), mlength Int, charmatches Int)
Declare @sn VarChar(25)
Declare @ln VarChar(25)
Declare @id Int
Declare @cnts Int 
Declare @cnt Int 
Declare @samechars Int 

Declare vCursor Cursor
    For Select id, ShopName, LocationName From #tbl
Open vCursor
Fetch Next From vCursor Into @id, @sn, @ln

While @@FETCH_STATUS = 0
Begin
   Set @cnts = 1
   Set @cnt = 1
   Set @samechars = 0
   While @cnt <= Case When Len(@sn) > Len(@ln) Then Len(@sn) Else Len(@ln) End
   Begin
      If Substring(@sn,@cnts,1) = Substring(@ln,@cnt,1) 
      Begin
        Set @cnts = @cnts + 1   --Only move this forward if there is a match
        Set @samechars = @samechars + 1
      End
      Else If Substring(@sn,@cnt,1) = Substring(@ln,@cnt,1) Set @samechars = @samechars + 1
      Set @cnt = @cnt + 1
   End

   Insert Into @results Select @id, @sn As ShopName, @ln As LocationName, Case When Len(@sn) > Len(@ln) Then Len(@sn) Else Len(@ln) End As mlength, @samechars As CharMatches 
   Fetch Next From vCursor Into @id, @sn, @ln
End

Select *, (Cast(charmatches As Float)/Cast(mlength As Float)) * 100.0 As percentmatch   From @results

Close vCursor
DeAllocate vCursor
Drop Table #tbl

结果:

id  ShopName    LocationName    mlength charmatches percentmatch
1   GreatMall   Great,Mall      10      9           90
2   Gingermall  Gingermall      10      10          100
3   MARK.HI     MARK,HI         7       6           85.7142857142857
4   GALLERY INC GALLERY. INC    12      11          91.6666666666667