Get subtotals in a Coldfusion group query - TechRepublic
Question
November 17, 2008 at 01:15 PM
mtownsend

Get subtotals in a Coldfusion group query

by mtownsend . Updated 17 years, 7 months ago

Hello. Say I have a table with the following data:

Acct#, Name, Cost
001, Bennett, 50.00
001, Bradley, 250.00
001, Green, 200.00
420, Smith, 50.00
420, Cummings, 50.00
470, Brown, 80.00

I would like the output to be like:

001-6405
Bennett 50.00
Bradley 250.00
Green 200.00
Total 001 500.00

420-6405
Smith 50.00
Cummings 50.00
Total 420 100.00

470-6405
Brown 80.00
Total 470 80.00

Total Accts 680.00

I used a query like this


SELECT SUM(Cost) AS CostSum, Acct, Name, Cost from travel WHERE reconciled = ‘No’ GROUP BY Acct, Name, Cost

to try and get the groups summed up together, but my sum total is only the first record so it looks like the following. The output is:


#Acct#-6405

#Name# #DollarFormat(Cost)#

Total: #DollarFormat(CostSum)#

001-6405
Bennett 50.00
Bradley 250.00
Green 200.00
Total 001 50.00

420-6405
Smith 50.00
Cummings 50.00
Total 420 50.00

470-6405
Brown 80.00
Total 470 80.00

Do I need to do some kind of a loop to get the subtotals for each account? Thanks.

This discussion is locked

All Comments