declare @Temp table(Code varchar(10), ID int, SInt int)
insert into @Temp(Code, ID, SInt)
select '01',1,3
union
select '01',2,3
union
select '01',2,3
union
select '02',1,3
union
select '02',3,5
union
select '02',2,6
union
select '03',3,5
union
select '03',1,6

select case when grouping(code)=1 then 'Grand Total:'
            
when grouping(ID)=1 then Code+' Total:'
            
else Code
       
end as LevelCode, ID, SUM(Sint) as S
from @Temp
group by Code, ID
with rollup

相关文章: