select isnull(F1,'合计'),sum(F2) F2
from ( select a.F1,sum(a.F2) F2 from B,A
where b.id=a.id group by F1
)DERIVEDTBL
group by F1 with rollup
from ( select a.F1,sum(a.F2) F2 from B,A
where b.id=a.id group by F1
)DERIVEDTBL
group by F1 with rollup
where b.id=a.id group by F1
select isnull(F1,'合计'),sum(F2) F2
from ( select F1,sum(F2) F2 from B group by F1
)DERIVEDTBL
group by F1 with rollup
A 1
B 2
A 3
B 4