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
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