I have two tables as below
NameId Name
11 Ancillary
22 Reviews
33 Audit
44 Logging
id NameId CountyId
51 11 1
52 11 1
53 11 2
54 22 2
55 22 3
56 33 3
57 33 3
Results should be total number of names associated to a county as below. Can the Nameid be dynamic meaning if more than 4 names are there query should be able to show the count automatically without hard coding row name. Also calculate total for each row at the end.
CountyId AncillaryCount ReviewsCount AuditCount LoggingCount Total
1 2 0 0 0 2
2 1 1 0 0 2
3 0 1 2 0 3