That example code seems overly complicated and redundant. Should the code be simpler like this:
SELECT id
, max(a) ma
, count(*) Cnt
FROM ...
GROUP BY id