@Symon - when you use aggregate functions" such as COUNT(), SUM, MAX(), etc, you need to indicate the columns that indicate a break/reset in the COUNT calculation, so SQL knows to count all T.id entries and display that count against the first C.id and C.name, then reset its internal counter to 0 and restart counting when C.id/C.name changes.