473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.
Jul 20 '05 #1
2 4422
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
--
Jul 20 '05 #2
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.
ne********@yahoo.com (Tech) wrote in message news:<35*************************@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.

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tech | last post by:
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...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: bala | last post by:
hi gurus have a employee table which consist of the normal employee related fields which the fields empid and supervisor mainly related to this question. have to create a single (the question...
4
by: Shahzad | last post by:
dear respected gurus, I would like to knew how to apply append,insert query for a self table where no primary keys issues. i do have problem say there are 5 rows of single record, this is data...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
6
by: Russell Warren | last post by:
Is there any better way to get a list of the public callables of self other than this? myCallables = classDir = dir(self) for s in classDir: attr = self.__getattribute__(s) if callable(attr)...
3
by: sks | last post by:
I have a table that contains keywords (Varchars) each one mapped to a product. so the database schema is just an id column, product column and keyword column. I want to select the products that...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.