I've sifted through posts to find an answer to my question, but I've had no luck. So I thought I would create a new one and see if the community might be able to help me out! Pardon me if I don't use the correct SQL terms in describing things.
Three tables in this problem. Table A is the "main" table with no dependencies. Table B has a foreign key reference to Table A. Table C has a foreign key reference to Table B.
There are two goals for this query. First is to sum up a field, let's say fieldC, in Table C. The only condition that must be met is a field in A, let's say fieldA, must be equal to, let's say X. To be fair, this solution is simple:
SELECT Sum(C.fieldC) FROM C
INNER JOIN B
ON C.foreign_keyB = B.id
INNER JOIN A
ON B.foreign_keyA = A.id
WHERE fieldA = X
OK, so now the second goal: I would like to count the number of rows found in Table A that match fieldA = X. I tried this:
SELECT Sum(C.fieldC), Count(A.id) FROM C
INNER JOIN B
ON C.foreign_keyB = B.id
INNER JOIN A
ON B.foreign_keyA = A.id
WHERE fieldA = X
But I'm getting totals that are way off. Is there some glaring mistake I'm making here? Thanks for the help everyone! =)