?
Solved

How do I get a subtotal of a group in a Coldfusion query?

Posted on 2008-11-17
4
Medium Priority
?
665 Views
Last Modified: 2013-12-20
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

<cfquery name="GetCostSum" datasource="travel">
      SELECT SUM(Cost) AS CostSum, Acct, Name, Cost from travel WHERE reconciled = 'No' GROUP BY Acct, Name, Cost
</cfquery>

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:

<cfoutput query="GetCostSum" group="Acct">
          <strong>#Acct#-6405</strong><br />
        <cfoutput>
              #Name# #DollarFormat(Cost)#<br />
        </cfoutput>
       Total: #DollarFormat(CostSum)#<br /><br />
         </cfoutput>

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.
0
Comment
Question by:jomark28
  • 2
4 Comments
 
LVL 16

Expert Comment

by:duncancumming
ID: 22980483

<cfoutput query="GetCostSum" group="Acct">
	<strong>#Acct#-6405</strong><br />
	<cfset total = 0>	<!--- reset for each new Acct --->
	<cfoutput>
		#Name# #DollarFormat(Cost)#<br />
		<cfset total = total + Cost>
	</cfoutput>
	Total: #DollarFormat(total)#<br /><br />
</cfoutput>

Open in new window

0
 
LVL 7

Accepted Solution

by:
black0ps earned 200 total points
ID: 22980521
There might be an easier way to do it with SQL, but i don't know what it is. I would just use a running tally.

<cfset runningtotal = 0>
<cfset grandtotal = 0>
<cfoutput query="GetCostSum" group="Acct">
<strong>#Acct#-6405</strong><br />
<cfoutput>
<cfset runningtotal = runningtotal + Cost>
#Name# #LSCurrencyFormat(Cost)#<br />
</cfoutput>
<cfset grandtotal = grandtotal + runningtotal>
Total: #LSCurrencyFormat(runningtotal)#<br /><br />
</cfoutput>
Grand Total: <cfoutput>#LSCurrencyFormat(grandtotal)#</cfoutput>
0
 
LVL 16

Expert Comment

by:duncancumming
ID: 22980542
also make sure you use ORDER BY Acct in your SQL.  
0
 

Author Closing Comment

by:jomark28
ID: 31517668
Thanks a lot! I just had to remember to reset the runningtotal to 0 after each Acct#.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question