I want to run a sql query to get the rows of one table and the number of references to it from another table. However the number of references should be splitt, by a field in the second table.
Here is my simplified DB-Schema
TableA TableB
------ ------
ID FK_A
GroupId
I want to query for TableA and count the referencing elements of TableB, but grouping them because of their GroupId. A result may look like this (Group1
, Group2
and Group3
are the values inside TableB.GroupId):
SELECT ID, ??? FROM TableA
ID Group1 Group2 Group3
1 1 0 7
2 3 3 9
I should count all of the occurrences in TableB, that reference my TableA.
I don't really know how to make the subquery or whatever is needed here.
UPDATE I do not know, which GroupIds can be in TableB. So multiple count statements aren't working.