If you did not include T.Client
in GROUP BY
, you can only include this field in an aggregate function. In your case, grouping by this field changes the logic, so the output (and is related to your attempt to group by CASE statement). Instead, wrap T.Client
in an aggregate function.
Thus, your groups are all the same, and when there is only one line, according to your CASE test test, you know what result the aggregate function will perform.
SELECT T.Post, ClientCount = COUNT(*) AS ClientCount, Client = CASE COUNT(*) WHEN 1 THEN MAX(T.Client) ELSE '[Clients]' END FROM MyTable T GROUP BY T.Post
source share