select id=case when grouping(id)=1 and grouping(date)=1 then '总计'
when grouping(id)=1 then date+':合计'
else convert(varchar(10),id) end,
debit=sum(debit),
credit=sum(credit),
date=isnull(date,'')
from @t
group by date,id
with rollup
create table tb(id int,debit numeric(6,1),credit numeric(10,2),date char(8))
insert tb select 1,8.0,9.0,'20051011'
union all select 2,4.0,5.6,'20051012'
union all select 3,2.0,3.6,'20051011'
select id,debit,credit,date1 from
(
select cast(id as varchar) id,debit,credit,date,date date1 from tb
union all
select '合计',sum(debit),sum(credit),date,''
from tb
group by date
) aa
order by date,id