Oh well, that's the
sneak-it-into-the-heading method shot, then.
If the table is small enough eg hundreds of rows, not millions, then probably simplest just to copy all records across from DB and, as part of this copy process, add subtotal and grand total lines after each group and at end.
But if you do that, you cannot re-sort the table on different columns - it would have to stay in the order that it was copied from/in/as/to.
Something like:
groupcode = "" 'or any other code guaranteed not to exist in database
groupcount = 0
grouptotal = 0
grandtotal = 0
for each record in database in groupcode order 'translate this pseudocode into SQL
if record.groupcode <> groupcode then
if groupcount <> 0 then
add subtotal row for groupcode grouptotal
end if
groupcode = record.groupcode
groupcount = 0
grouptotal = 0
end if
add row for record.groupcode record.groupquantity
increment groupcount
add record.quantity to grouptotal
add record.quantity to grandtotal
next
if groupcount <> 0 then
add subtotal row for groupcode grouptotal
end if
add grand total row for grandtotal