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

Query performance Call Center data (20h)

Looking for on Premises tool to read data from SQL logs and populate data warehouse (1d)

Possible SQL 2014 to 2016 Issue - Some Application Functions Slow/Failing (1d)

Working with multiple WHERE statements (1d)

Create a new field value that shows a difference in value from 2 fields (3d)

Hierarchy wise Sales Targets (3d)

Get the MaxDate in results (5d)

Switch Statement within a SSRS SQL Expression (6d)

- Advertisement -