The above query by apegram is in general good, except for sort by cost. Typically if there was credit or discount (negative cost). Also grand total shows ugly harcoded custID.
The solution is slight modification to the same, though credit goes to apegram for writing the workable query.
SELECT CustID, Items, Cost, ExtraCharges FROM (
SELECT '1' orderCol, CustID, Items, Cost, ExtraCharges FROM Sales
UNION
SELECT '2' orderCol, CustID, 'Subtotal' as Items, Sum(Cost) as Cost, Sum(ExtraCharges) as ExtraCharges
FROM Sales
GROUP BY CustID
UNION
SELECT '99' orderCol, '' as CustID, 'GrandTotal' as Items, Sum(Cost) as Cost, Sum(ExtraCharges) as ExtraCharges
FROM Sales
) as t1
ORDER BY CASE WHEN orderCol=99 THEN 1 ELSE 0 END, CustID, orderCol, items
Reputation Points: 53
Solved Threads: 20
Junior Poster in Training
Offline 82 posts
since Aug 2009