Unix Technical Forum

Self Join Question

This is a discussion on Self Join Question within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a table tblEmails where the columns are id,list_id,address_id. I have many lists. I need to find out ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:28 AM
Tech
 
Posts: n/a
Default Self Join Question

I have a table tblEmails where
the columns are id,list_id,address_id. I have many lists. I need to
find out
if a couple of lists (list_ids - 1000,1001,1002) have same
address_ids in common or not.


Snaphot

1,1000,1234
2,1000,2345
3,1001,4567
4,1001,1234

now the query should return 1 record if I give 1000 and 1001 as
list_ids.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:28 AM
David Portas
 
Posts: n/a
Default Re: Self Join Question

Did you really want a self-join query or do you just want to know which
addresses appear in more than one list?

Is (address_id, list_id) unique? If so, you should be able to do it this
way:

SELECT address_id
FROM Emails
WHERE list_id IN (1000,1001)
GROUP BY address_id
HAVING COUNT(*)>1

If (address_id, list_id) isn't unique, then:

SELECT address_id
FROM Emails
WHERE list_id IN (1000,1001)
GROUP BY address_id
HAVING MIN(list_id)<MAX(list_id)

It helps if you post proper DDL (CREATE TABLE statement) for your table and
include any keys and constraints. That way we don't have to guess at
signifcant aspects of your data.

I guess you could also do it with a self join like this:

SELECT DISTINCT E1.address_id
FROM Emails AS E1
JOIN Emails AS E2
ON E1.address_id = E2.address_id
AND E1.list_id=1000
AND E2.list_id=1001

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 02:29 AM
Fred
 
Posts: n/a
Default Re: Self Join Question

Select tblEmails.list_id
From tblEmails INNER JOIN
tblEmails tblEmails_1 ON tblEmails.list_id = tblEmails_1.list_id
WHERE (tblEmails.list_id = 1000)

or something very similar.


neeraj_bod@yahoo.com (Tech) wrote in message news:<35130298.0404141052.c45a143@posting.google.c om>...
> I have a table tblEmails where
> the columns are id,list_id,address_id. I have many lists. I need to
> find out
> if a couple of lists (list_ids - 1000,1001,1002) have same
> address_ids in common or not.
>
>
> Snaphot
>
> 1,1000,1234
> 2,1000,2345
> 3,1001,4567
> 4,1001,1234
>
> now the query should return 1 record if I give 1000 and 1001 as
> list_ids.

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 08:12 AM.


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