So suctami na konci:
declare @tTemp table (Skupina int, Nazev char(10), Pocet int)
insert into @tTemp (Nazev, Skupina, Pocet) values
('Jedna', 1, 10),
('Dva', 4, 15),
('Tri', 4, 5),
('Ctyri', 2, 12)
SELECT ID, Skupina, Pocet = Sum(Pocet)
FROM (select ID = ROW_NUMBER() OVER (ORDER BY Skupina, Pocet), * from @tTemp) a
GROUP BY
GROUPING SETS (
(Skupina, ID),
(Skupina),
() )
ORDER BY
Skupina,
CASE
WHEN ID IS NULL THEN 1
ELSE 0
END, ID
;