SQL - similar data in column

前端 未结 3 680
野的像风
野的像风 2021-01-20 15:52

Is there any way to find similar results in column. Example:

I want query return from table data without 4 green tree because there is no similar data to g

3条回答
  •  鱼传尺愫
    2021-01-20 16:06

    This approach uses a very basic notion of similarity but can be extended to a better definition. It's not very efficient, mind you. The count(1) + 1 includes the base phrase.

    create table phrases ( phrase varchar(max) )
    insert phrases values( 'blue car' ), ( 'blue cars' ), ('green tree' ), ( 'red doll' ), ( 'red dolly' )
    
    create function dbo.fnSimilar( @s1 varchar(max), @s2 varchar(max) )
    returns int
    begin
        if @s1 = @s2 return 0 -- a phrase is not similar to itself
        if @s1 like @s2 + '%' return 1
        if @s2 like @s1 + '%' return 2
        return 0
    end
    
    select x.phrase, similar = count(1) + 1 from 
    (
        select p1.phrase from phrases p1
        inner join phrases p2 on dbo.fnSimilar( p2.phrase, p1.phrase ) = 1
    ) x
    group by x.phrase
    

    Result:

    phrase      similar
    --------    -------
    blue car    2
    red doll    2
    

提交回复
热议问题