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!

Criteria question

Status
Not open for further replies.

bnageshrao

Programmer
Mar 17, 2001
83
US
Hi

I have two tables one is called lead and another one is called contact. Companyid is the key in both tables. For each lead there can be either one lead or more than one lead. I would like to find out lead with more than one contacts. I did the grouping based on company ids and pulled the contact names. It shows for each companyid either one or more contacts. What criteria I should use to get just company with more than one contacts. Do I have to use Group By and along with other criteria like having etc. I just dont know the syntax. If some one can shed some light into this, I will be gratefull, thanks in advance.
 
This assumes your table is called Contact
One Field name is CompanyID
Another Field name is ContactID


SELECT Contact.CompanyID, Count(Contact.ContactID) AS ContactCount
FROM Contact
GROUP BY Contact.CompanyID
HAVING (((Count(Contact.ContactID))>1));

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
If you want all the contact names of all leads with more than one contacts:
SELECT L.*, C.*
FROM (lead AS L
INNER JOIN contact AS C ON L.Companyid = C.Companyid)
INNER JOIN (SELECT Companyid FROM contact GROUP BY Companyid HAVING Count(*)>1
) AS G ON L.Companyid = G.Companyid

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top