This seems like a regular thing to do, but I can't seem to find how to do it.
I have a join query
SELECT a.nom_batim, COUNT(b.maxten) AS NumFaulty
FROM tblTrials AS b, tblRooms AS a
WHERE b.batiment = a.batiment
AND b.maxten > 10
GROUP BY a.nom_batim
ORDER BY a.nom_batim
that should only return a count of the tblTrials entries. However, since I don't know how to code that, it's currently counting all occurances of b.maxten > 10
, TIMES all occurances of b.batiment = a.batiment
. I have 1 actual occurance of b.maxten > 10
in the table, but 231 occurances of b.batiment = a.batiment (the tables are set up badly, not my choice; these tables are considered read-only to me), so it returns a count of 231.
How do I COUNT(b.maxten)
correctly, but still display a.nom_batim
as a user-friendly representation of the batiment
ID field? (a.nom_batim
is the long name for the building #batiment
)
UPDATE
This is what I ended up doing so far..
SELECT a.nom_batim, COUNT(b.batiment) AS NumFaulty
FROM (SELECT DISTINCT nom_batim, batiment FROM tblRooms) AS a
INNER JOIN tblTrials AS b ON a.batiment = b.batiment
WHERE b.maxten > 10
GROUP BY a.nom_batim
ORDER BY a.nom_batim
It works but seems like a resource hog when I only need max ~30 values from tblRooms, but have to query all 5000+ rows selecting only distinct batiment
values. Is there no way to do this without having a batiment
table tblBatiment: batiment, nom_batim
I know it's the best way but I don't have the access.