MustangPriMe
IS-IT--Management
I've apparently forgotten how to use the GROUP BY clause effectively.
I have 3 tables relating to each other - tblCompanies, tblSites and tblContacts.
tblCompanies
- fldCompanyID
- fldCompanyName
- etc
tblSites
- fldSiteID
- fldOwningCompanyID
- fldAddress
- etc
tblContacts
- fldContactID
- fldOwningSiteID
- fldName
- fldContactArchived
- etc
I'm trying to query the database to list how many sites per company have non-archived (or live) contacts
I'm using..
SELECT tblSites.fldOwningCompanyID, Count(tblSites.fldSiteID) AS CountOffldSiteID
FROM tblContacts INNER JOIN tblSites ON tblContacts.fldOwningSiteID = tblSites.fldSiteID
WHERE (tblContacts.fldContactArchived=0)
GROUP BY tblSites.fldOwningCompanyID;
.. which returns a company ID per row, but gives the count of live contacts for each company instead of the number of sites which have live contacts for each company.
I can't think where I'm going wrong. The only thing I can think is that it's something to do with the fact that the tables are actually linked tables from an SQL database, and for some reason the GROUP BY clause is not working in the same way.
Can anyone put me out of my misery?
I have 3 tables relating to each other - tblCompanies, tblSites and tblContacts.
tblCompanies
- fldCompanyID
- fldCompanyName
- etc
tblSites
- fldSiteID
- fldOwningCompanyID
- fldAddress
- etc
tblContacts
- fldContactID
- fldOwningSiteID
- fldName
- fldContactArchived
- etc
I'm trying to query the database to list how many sites per company have non-archived (or live) contacts
I'm using..
SELECT tblSites.fldOwningCompanyID, Count(tblSites.fldSiteID) AS CountOffldSiteID
FROM tblContacts INNER JOIN tblSites ON tblContacts.fldOwningSiteID = tblSites.fldSiteID
WHERE (tblContacts.fldContactArchived=0)
GROUP BY tblSites.fldOwningCompanyID;
.. which returns a company ID per row, but gives the count of live contacts for each company instead of the number of sites which have live contacts for each company.
I can't think where I'm going wrong. The only thing I can think is that it's something to do with the fact that the tables are actually linked tables from an SQL database, and for some reason the GROUP BY clause is not working in the same way.
Can anyone put me out of my misery?