PATINDEX与SOUNDEX

时间:2018-11-28 12:01:35

标签: sql-server tsql sql-server-2008-r2

想使用PATINDEX和SOUNDEX搜索字符串。

我有一张带有一些示例数据的下表,用于使用PATINDEXSOUNDEX搜索给定的字符串。

create table tbl_pat_soundex
(
    col_str varchar(max)
);

insert into tbl_pat_soundex values('Smith A Steve');
insert into tbl_pat_soundex values('Steve A Smyth');
insert into tbl_pat_soundex values('A Smeeth Stive');
insert into tbl_pat_soundex values('Steve Smith A');
insert into tbl_pat_soundex values('Smit Steve A');

要搜索的字符串:-'史密斯史蒂夫'

SELECT col_str,PATINDEX('%Smith%',col_str) [Smith],PATINDEX('%A%',col_str) [A],PATINDEX('%Steve%',col_str) [Steve]
FROM tbl_pat_soundex

获取输出:

col_str         Smith   A   Steve
---------------------------------
Smith A Steve   1       7   9
Steve A Smyth   0       7   1
A Smeeth Stive  0       1   0
Steve Smith A   7       13  1
Smit Steve A    0       12  6

预期输出:

col_str         Smith   A   Steve
---------------------------------
Smith A Steve   1       7   9
Steve A Smyth   9       7   1
A Smeeth Stive  3       1   10
Steve Smith A   7       13  1
Smit Steve A    1       12  6

尝试:

SELECT col_str,
        PATINDEX('%'+soundex('Smith')+'%',soundex(col_str)) [Smith],
        PATINDEX('%'+soundex('A')+'%',soundex(col_str)) [A],
        PATINDEX('%'+soundex('Steve')+'%',soundex(col_str)) [Steve]
FROM tbl_pat_soundex    

但是得到意想不到的结果:

col_str         Smith   A   Steve
---------------------------------
Smith A Steve   1       0   0
Steve A Smyth   0       0   1
A Smeeth Stive  0       1   0
Steve Smith A   0       0   1
Smit Steve A    1       0   0   

注意:我在表中有100 Millions条记录可供搜索。

1 个答案:

答案 0 :(得分:1)

这是一个选择,不确定要考虑所有要做的工作,如何处理1亿条记录。您必须进行测试。

从高层次上我理解这是您基本需要的

  • 根据另一个字符串的单词搜索字符串中的所有单词
  • 返回原始字符串中字符等于或听起来像搜索词的字符起始位置。

您可以使用DIFFERENCE()进行比较:

  

DIFFERENCE比较两个不同的SOUNDEX值,并返回   整数值。此值衡量SOUNDEX值的程度   匹配,范围为0到4。值为0表示弱或否   SOUNDEX值之间的相似性; 4表示非常相似,   甚至完全相同的SOUNDEX值。

您需要基于空格''分割字符串,并且从2008年开始,您必须滚动自己的函数。

在这里,我使用https://sqlperformance.com/2012/07/t-sql-queries/split-strings的XML函数作为示例,如果您自己拥有或想要使用其他功能,显然您需要进行调整:

CREATE FUNCTION dbo.SplitStrings_XML
(
   @List       NVARCHAR(MAX),
   @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
   RETURN 
   (  
      SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(@List, @Delimiter, '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );
GO

我切换并使用表变量来显示示例,我建议不要使用您拥有的数据量来创建和使用物理表。

选项1-非动态:

DECLARE @tbl_pat_soundex TABLE
    (
        [col_str] VARCHAR(MAX)
    );

INSERT INTO @tbl_pat_soundex
VALUES ( 'Smith A Steve' )
,( 'Steve A Smyth' )
,( 'A Smeeth Stive' )
,( 'Steve Smith A' )
,( 'Smit Steve A' )

SELECT DISTINCT [aa].[col_str]
              , MAX([aa].[Smith]) OVER ( PARTITION BY [aa].[col_str] ) AS [Smith]
              , MAX([aa].[A]) OVER ( PARTITION BY [aa].[col_str] ) AS [A]
              , MAX([aa].[Steve]) OVER ( PARTITION BY [aa].[col_str] ) AS [Steve]
FROM   (
           SELECT      [a].[col_str]
                     , CASE WHEN DIFFERENCE([b].[item], 'Smith') = 4 THEN
                                CHARINDEX([b].[item], [a].[col_str])
                            ELSE 0
                       END AS [Smith]
                     , CASE WHEN DIFFERENCE([b].[item], 'A') = 4 THEN
                                CHARINDEX([b].[item], [a].[col_str])
                            ELSE 0
                       END AS [A]
                     , CASE WHEN DIFFERENCE([b].[item], 'Steve') = 4 THEN
                                CHARINDEX([b].[item], [a].[col_str])
                            ELSE 0
                       END AS [Steve]
           FROM        @tbl_pat_soundex [a]
           CROSS APPLY [dbo].[SplitStrings_XML]([a].[col_str], ' ') [b]
       ) AS [aa];
  • 使用该函数,我们将字符串分成单个单词
  • 然后我们使用case语句检查DIFFERENCE值
  • 如果该DIFFERENCE值等于4,则我们将原始单词的CHARINDEX值返回字符串。
  • 如果不相等,我们返回0

然后从那里开始,根据原始字符串获取每个值的最大值:

          , MAX([aa].[Smith]) OVER ( PARTITION BY [aa].[col_str] ) AS [Smith]
          , MAX([aa].[A]) OVER ( PARTITION BY [aa].[col_str] ) AS [A]
          , MAX([aa].[Steve]) OVER ( PARTITION BY [aa].[col_str] ) AS [Steve]

为了获得最终结果:

enter image description here

选项2-具有枢轴的动态:

我们将声明要搜索的字符串,将其拆分并搜索原始字符串中的各个单词,然后旋转结果。

--This example is using global temp tables as it's showing how
--to build a dynamic pivot
IF OBJECT_ID('tempdb..##tbl_pat_soundex') IS NOT NULL
  DROP TABLE [##tbl_pat_soundex];

IF OBJECT_ID('tempdb..##tbl_col_str_SearchString') IS NOT NULL
  DROP TABLE [##tbl_col_str_SearchString];

CREATE TABLE [##tbl_pat_soundex]
    (
        [col_str] VARCHAR(MAX)
    );

INSERT INTO [##tbl_pat_soundex]
VALUES ( 'Smith A Steve' )
     , ( 'Steve A Smyth' )
     , ( 'A Smeeth Stive' )
     , ( 'Steve Smith A' )
     , ( 'Smit Steve A' );

--What are you searching for?
DECLARE @SearchString NVARCHAR(200);
SET @SearchString = N'Smith A Steve';

--We build a table we load with every combination of the words from the string and the words from the SearchString for easier comparison.
CREATE TABLE [##tbl_col_str_SearchString]
    (
        [col_str] NVARCHAR(MAX)
      , [col_str_value] NVARCHAR(MAX)
      , [SearchValue] NVARCHAR(200)
    );

--Load that table for comparison
--split our original string into individual words
--also split our search string into individual words and give me all combinations.
INSERT INTO [##tbl_col_str_SearchString] (
                                             [col_str]
                                           , [col_str_value]
                                           , [SearchValue]
                                         )
            SELECT      DISTINCT [a].[col_str]
                               , [b].[item]
                               , [c].[item]
            FROM        [##tbl_pat_soundex] [a]
            CROSS APPLY [dbo].[SplitStrings_XML]([a].[col_str], ' ') [b]
            CROSS APPLY [dbo].[SplitStrings_XML](@SearchString, ' ') [c]
            ORDER BY    [a].[col_str];

--Then we can easily compare each word and search word for those that match or sound alike using DIFFERNCE()
SELECT [col_str], [col_str_value], [SearchValue], CASE WHEN DIFFERENCE([col_str_value], [SearchValue]) = 4 THEN CHARINDEX([col_str_value], [col_str]) ELSE 0 END AS [Match] FROM ##tbl_col_str_SearchString

--Then we can pivot on it
--and we will need to make it dynamic since we are not sure what what @SearchString could be.
DECLARE @PivotSQL NVARCHAR(MAX);
DECLARE @pivotColumn NVARCHAR(MAX);

SET @pivotColumn = N'[' + REPLACE(@SearchString, ' ', '],[') + N']';

SET @PivotSQL = N'SELECT * FROM (
SELECT [col_str], [SearchValue], CASE WHEN DIFFERENCE([col_str_value], [SearchValue]) = 4 THEN CHARINDEX([col_str_value], [col_str]) ELSE 0 END AS [Match] FROM ##tbl_col_str_SearchString
) aa
PIVOT (MAX([Match]) FOR [SearchValue] IN (' + @pivotColumn
                + N')) AS MaxMatch
ORDER BY [MaxMatch].[col_str]
';

--Giving us the final results.
EXEC sp_executesql @PivotSQL