SQL: Find rows that match closely but not exactly SQL: Find rows that match closely but not exactly postgresql postgresql

SQL: Find rows that match closely but not exactly


SELECT * FROM countriesWHERE country = 'sweden'OR sport = 'basketball'OR year = 1998ORDER BYcast(country = 'sweden' AS integer) +cast(sport = 'basketball' as integer) + cast(year  = 1998 as integer) DESC

It's not beautiful, but well. You can cast the boolean expressions as integers and sum them.

You can easily change the weight, by adding a multiplicator.

cast(sport = 'basketball' as integer) * 5 + 


This is how I would do it ... the multiplication factors used in the case stmts will handle the importance(weight) of the match and they will ensure that those records that have matches for columns designated with the highest weight will come up top even if the other columns don't match for those particular records.

/*-- Initial Setup -- drop table sport create table sport (id int, Country varchar(20) , sport varchar(20) , yr int )insert into sport values (1,'USA','basketball','1956'),(2,'Sweden','basketball','1998'),(3,'Sweden','skating','1998'),(4,'Switzerland','golf','2001')select * from sport*/select * ,         CASE WHEN Country='sweden'      then 1 else 0 end * 100 +         CASE WHEN sport='basketball'    then 1 else 0 end * 10  +         CASE WHEN yr=1998               then 1 else 0 end * 1       as Matchfrom sportWHERE    country = 'sweden'OR sport   = 'basketball'OR yr      = 1998ORDER BY Match Desc


It might help if you wrote a stored procedure that calculates a "similarity metric" between two rows. Then your query could refer to the return value of that procedure directly rather than having umpteen conditions in the where-expression and the order-by-expression.