Not sure if this is what you are looking for, here is how it could be achieved with join : BUT with assumptions ... that your tables look like a sample that I used as a demo. If this is not the case, please share a tabular chart and data samples with us with the expected results ...
http://sqlfiddle.com/#!2/1e65c/2
SELECT A.ID, A.NAME, CASE WHEN B.AID = A.ID THEN COUNT(*) END AS SUM1, CASE WHEN B.AID = A.ID THEN COUNT(*) END AS SUM1 FROM A INNER JOIN B ON A.ID = B.AID INNER JOIN C ON B.AID = C.AID GROUP BY A.ID, A.NAME ; SELECT A.ID, A.NAME, COUNT(B.AID) AS SUM1, COUNT(C.AID) AS SUM1 FROM A INNER JOIN B ON A.ID = B.AID INNER JOIN C ON B.AID = C.AID GROUP BY A.ID, A.NAME ; | ID | NAME | SUM1 |
source share