0

I have a query like this

SELECT * from table1 where 
CONTAINS(column,'key1 OR key2 OR key3')

Now i want to add another column which contains the number of the keys(key1,key2,key3) found in that row(either 1,2 or 3).How do i do that? I don't want to use 'containstable' or freetexttable as i will making my own ranking function once i get the no of keys matched.

4

1 回答 1

1

You can get the number of keys that match easily enough:

select *,
  case when Contains( Column, 'key1' ) then 1 else 0 end +
  case when Contains( Column, 'key2' ) then 1 else 0 end +
  case when Contains( Column, 'key3' ) then 1 else 0 end as [NumberOfMatchingKeys]
  from Table1
  where Contains( column, 'key1 OR key2 OR key3' )

What you can't get, at least easily, is the number of matches for any given key. Finding out that key2 matched 7 times is a problem.

A possible optimization is to remove the Contains from the where clause:

select * from (
  select *,
    case when Contains( Column, 'key1' ) then 1 else 0 end +
    case when Contains( Column, 'key2' ) then 1 else 0 end +
    case when Contains( Column, 'key3' ) then 1 else 0 end as [NumberOfMatchingKeys]
    from Table1 ) as Louise
 where NumberOfMatchingKeys > 0
于 2013-05-04T00:32:35.803 回答