Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced selection help

Status
Not open for further replies.

gbrian

IS-IT--Management
Sep 22, 2004
96
US
Fields: contactID, siteID

Each site may be associated with 1 to 500 contactID's. What I am trying to do is select 2 contact ID's for each siteID.

Any ideas?
 
something like:

Code:
SELECT SiteID, ContactID FROM [b]TableName[/b] A Where ContactID in (SELECT TOP 2 ContactID FROM [b]TableName[/b] B Where A.SiteID = B.SiteID)

copy and paste in the SQL view and change the TableName.

Leslie
 
That helped a little--the problem is that will select 2 contact ID's each time it finds that site ID...how can I use a distinct list of my site id's to do this?
 
I don't understand what you mean.

Can you show some sample data, what results you are getting compared to the results you want.


Leslie
 
Here is an example of one site that is returned:

SITE_ID CONTACT_ID
0000241027 00000000558349
0000241027 00000000558347
0000241027 00000000558347
0000241027 00000000558349
0000241027 00000000558349
0000241027 00000000558349
0000241027 00000000558347
0000241027 00000000558347
 
You may try SELECT DISTINCT

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
May be two distinct(asc/desc) query and a union query of the distict queries.
Or someone might have an easier way.

Zameer Abdulla
Jack of Visual Basic Programming, Master in Dining & Sleeping
Visit Me
 
like PHV stated:
Code:
SELECT DISTINCT SiteID, ContactID FROM TableName A Where ContactID in (SELECT TOP 2 ContactID FROM TableName B Where A.SiteID = B.SiteID)

Leslie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top