What I need to do is to get a total of the TotalWordCount column where CG.IsToggled = 1
You cannot do a total of a total (aggregate of an aggregate) in the same query. But, you can do it in a subquery like this :
SELECT CG.Id, CG.Name, CG.IsToggled, SUM(c2.TotalWordCount) AS TotalWordCount
FROM CategoryGroup AS CG
INNER JOIN
(
SELECT Id, Name, COUNT(*) AS TotalWordCount
FROM CategoryGroup
WHERE IsToggled = 1
GROUP BY Id, Name
) AS c2 ON c2.Id = CG.Id AND c2.Name = CG.Name
JOIN Category AS C ON CG.Id = C.GroupId
JOIN Phrase AS P ON C.Id = P.CategoryId
GROUP BY CG.Id, CG.Name, CG.IsToggled
ORDER BY CG.Name
Note that: You might need to use LEFT JOIN
to include those with IsToggled = 0
in the query as well:
SELECT CG.Id, CG.Name, CG.IsToggled, SUM(COALESCE(c2.TotalWordCount, 0)) AS TotalWordCount
FROM CategoryGroup AS CG
JOIN Category AS C ON CG.Id = C.GroupId
JOIN Phrase AS P ON C.Id = P.CategoryId
LEFT JOIN
(
SELECT Id, Name, COUNT(*) AS TotalWordCount
FROM CategoryGroup
WHERE IsToggled = 1
GROUP BY Id, Name
) AS c2 ON c2.Id = CG.Id AND c2.Name = CG.Name
GROUP BY CG.Id, CG.Name, CG.IsToggled
ORDER BY CG.Name