SQLTeam.com Logo

Return to Union of two full text queries without duplicates

Union of two full text queries without duplicates

Written by Bill Graziano on 23 October 2000

David writes "Hi SQL Guru, I am trying to union two queries into one result set using union. Both queries are full text searches using ContainsTable on different tables in the same database. There is one full text catalog shared by both tables. My problem is that I am getting rows that are duplicates in all columns except FullTextTable.Rank . . .

The question continues "I read your artical http://www.sqlteam.com/item.asp?ItemID=289 but using distinct won't work because the duplicate rows are introduced by the union (I tried it out anyway, didn't work with a union).

I also checked out http://www.sqlteam.com/item.asp?ItemID=239 however this won't work either because the FullTextTable.Rank prevents exact duplicates.

Is there a way to not get duplicate rows from unioning full text queries?

SQL server 7 standard edition w/ sp2
NT4 server w/ sp6

Thanks,
Dave"


First of all my humble apologies for not finding this question sooner. Unfortunately I'm just way behing but catching up. Second, many, many thanks for looking at previous articles before you posted your question. I'm probably a little late in answering but I'm just so excited that someone tried to look up the answer before posting a question that I'm going to give it a shot anyway.

How about using a temp table? Put all your Primary Keys and Ranks into a temp table. Then run a Group by query.

CREATE TABLE #QueryResult (ID int, Rank int)

INSERT #QueryResult
SELECT YourPrimaryKey, FullTextTable.Rank
FROM . . . (and the rest of your query)

INSERT #QueryResult
SELECT YourPrimaryKey, FullTextTable.Rank
FROM . . . (and the rest of your SECOND query)

Select ID, Max(Rank) as Rank
From #QueryResults
Group by ID


This should get each entry once with the highest rank returned? Hope this helps.
-graz.