Unix Technical Forum

eliminating (inverse) duplicates in result

This is a discussion on eliminating (inverse) duplicates in result within the SQL Server forums, part of the Microsoft SQL Server category; --> Am I going about this the right way? I want to find pairs of entities in a table that ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:10 PM
Kerry Tomlinson
 
Posts: n/a
Default eliminating (inverse) duplicates in result

Am I going about this the right way? I want to find pairs of entities
in a table that have some relationship (such as a field being the
same), so I

select t1.id, t2.id from sametable t1 join sametable t2 on
t1.id<>t2.id
where t1.fieldx=t2.fieldx ...

The trouble is, this returns each pair twice, e.g.

B C
C B
M N
N M

Is there a way to do this kind of thing and only get each pair once?

Kerry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:10 PM
David Portas
 
Posts: n/a
Default Re: eliminating (inverse) duplicates in result

Try this:

SELECT t1.id, t2.id
FROM sametable t1
JOIN sametable t2
ON t1.id < t2.id
WHERE t1.fieldx=t2.fieldx ...

(a subtle difference in the ON clause)

--
David Portas
------------
Please reply only to the newsgroup
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:10 PM
Kerry Tomlinson
 
Posts: n/a
Default Re: eliminating (inverse) duplicates in result

Many thanks for the tip. Very elegant. Makes the join half as big too!

Thanks,

Kerry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:40 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com