472,126 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

union with Rank create duplicated records

Hi Everyone.

I have a problem that I could not figure out what to do with it.

I have a couple of tables and views. All have the same unique ID.
When a user try to do a search on our web site we want to try to look in multiple tables and views, Rank the results and aggregate the results back (remove any duplicates).

Part of the search is a Full Text Index search.

I created already the following query that seems to be working ok except for one problem, some of the results are duplicated because the Rank is unique.


select rank,[key],PL
From freetexttable (dbo.items,*,'Key Words') as ft
join Items as I on ft.[Key] = I.ItemNumber
LEFT OUTER JOIN dbo.IM_90_UDF_IMMasterfile ON ft.[Key]= dbo.IM_90_UDF_IMMasterfile.ItemNumber
Where (I.Approved = 1 and dbo.IM_90_UDF_IMMasterfile.Web_Exclude<>'Y')

union

select '999999',ItemNumber,PL
from View_Items_Published
Where (Mfg_Part_Number like '%Key Words%')

union

select '999998',ItemNumber,PL
from View_Items_Published
Where (ItemNumber like '%Key Words%')

order by rank desc


In a way to fix this issue I was suggested to create a Temp table, insert the data from all tables / views into it and group the results.
The problem with that is I have to Drop the table before/after every search. And lots of other issues (table locks etc’)

Here is the script for this one:

Drop table #QueryResult
CREATE TABLE #QueryResult (Rank int,ItemNumber char(15), PL Char(4))

INSERT #QueryResult (Rank ,ItemNumber , PL)
select rank,I.ItemNumber ,PL
From freetexttable (dbo.items,*,'Key Words') as ft
join Items as I on ft.[Key] = I.ItemNumber
LEFT OUTER JOIN dbo.IM_90_UDF_IMMasterfile ON ft.[Key]= dbo.IM_90_UDF_IMMasterfile.ItemNumber
Where (I.Approved = 1 and dbo.IM_90_UDF_IMMasterfile.Web_Exclude<>'Y')


INSERT #QueryResult
select '999999',ItemNumber,PL
from View_Items_Published
Where (Mfg_Part_Number like '%Key Words%')

INSERT #QueryResult
select '999998',ItemNumber,PL
from View_Items_Published
Where (ItemNumber like '%Key Words%')


Select ItemNumber, Max(Rank) as Rank, PL
From #QueryResult
Group by ItemNumber, PL
order by rank desc



My question is:
Any way of aggregating all the results on the fly, I’m sure there is something better.
Thanks in advance

Oren Levy
Dec 7 '06 #1
2 2743
iburyak
1,017 Expert 512MB
Try this:

[PHP]select max(rank), [key],PL -- you can change it to min(rank)
FROM(
select rank,[key],PL
From freetexttable (dbo.items,*,'Key Words') as ft
join Items as I on ft.[Key] = I.ItemNumber
LEFT OUTER JOIN dbo.IM_90_UDF_IMMasterfile ON ft.[Key]= dbo.IM_90_UDF_IMMasterfile.ItemNumber
Where (I.Approved = 1 and dbo.IM_90_UDF_IMMasterfile.Web_Exclude<>'Y')

union

select '999999',ItemNumber,PL
from View_Items_Published
Where (Mfg_Part_Number like '%Key Words%')

union

select '999998',ItemNumber,PL
from View_Items_Published
Where (ItemNumber like '%Key Words%')
) a
GROUP by [key],PL
order by rank desc[/PHP]
Dec 7 '06 #2
Works like a charm.

Thanks


Try this:

[PHP]select max(rank), [key],PL -- you can change it to min(rank)
FROM(
select rank,[key],PL
From freetexttable (dbo.items,*,'Key Words') as ft
join Items as I on ft.[Key] = I.ItemNumber
LEFT OUTER JOIN dbo.IM_90_UDF_IMMasterfile ON ft.[Key]= dbo.IM_90_UDF_IMMasterfile.ItemNumber
Where (I.Approved = 1 and dbo.IM_90_UDF_IMMasterfile.Web_Exclude<>'Y')

union

select '999999',ItemNumber,PL
from View_Items_Published
Where (Mfg_Part_Number like '%Key Words%')

union

select '999998',ItemNumber,PL
from View_Items_Published
Where (ItemNumber like '%Key Words%')
) a
GROUP by [key],PL
order by rank desc[/PHP]
Dec 13 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by (Pete Cresswell) | last post: by
32 posts views Thread by sandy | last post: by
2 posts views Thread by Fred Zuckerman | last post: by
2 posts views Thread by M Stery | last post: by
2 posts views Thread by mattytee123 | last post: by
2 posts views Thread by Thomas | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.