Union of two full text queries without duplicates

By Bill Graziano on 23 October 2000 | Tags: SELECT


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.


Related Articles

Joining to the Next Sequential Row (2 April 2008)

Writing Outer Joins in T-SQL (11 February 2008)

How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007)

How to Use GROUP BY in SQL Server (30 July 2007)

SQL Server 2005: Using OVER() with Aggregate Functions (21 May 2007)

Server Side Paging using SQL Server 2005 (4 January 2007)

Using XQuery, New Large DataTypes, and More (9 May 2006)

Counting Parents and Children with Count Distinct (10 January 2006)

Other Recent Forum Posts

I have a sql query which returns a sum of total amount by user and I am trying to add another column which uses the total and multiply it by a number (22h)

Troubleshooting Deadlocks in SQL Server (10d)

Last Login date and time (11d)

Negative effects of High VLF counts (11d)

Need to return a value that indicates that a record has been added, but not when a record is modified (12d)

Indexex on low cardinality fields (13d)

Error in stored procedure (13d)

Spam post flagging (13d)

- Advertisement -