Computing the Sum of a Recordset

# Thread: Computing the Sum of a Recordset

1. Member
Join Date
Dec 1969
Posts
37

## Computing the Sum of a Recordset

Is there some way for me to compute the sum of a recordset in ASP? <BR><BR>Thanks

2. Senior Member
Join Date
Dec 1969
Posts
102

## RE: Computing the Sum of a Recordset

look up the SUM command in SQL<BR>eg: Select Sum(Numbers) as Total From Table

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## If you are going to need to...

...show/use the members of the recordset in the HTML output, then yes, you could use a separate SQL SUM(). But more efficient would be to simply calculate the sum on the fly, as you process the records.<BR><BR>Example:<BR><BR>&#060;TABLE Border=1 CellPadding=5&#062;<BR>&#060;TR&#062;<BR> &#060;TH&#062;Product&#060;/TH&#062;<BR> &#060;TH&#062;Quantity&#060;/TH&#062;<BR> &#060;TH&#062;Price Each&#060;/TH&#062;<BR> &#060;TH&#062;Cost&#060;/TH&#062;<BR>&#060;/TR&#062;<BR>&#060;%<BR>SQL = "SELECT product, quantity, price FROM itemsSold WHERE buyer = &#039;Joe&#039; "<BR>Set RS = yourConnection.Execute( SQL )<BR><BR>total = 0.0<BR><BR>Do Until RS.EOF<BR> qty = RS("quantity")<BR> price = RS("price")<BR> cost = qty * price <BR> total = total + cost<BR>%&#062;<BR> &#060;TR&#062;<BR> &#060;TD&#062;&#060;%=RS("product")%&#062;&#060 ;/TD&#062;<BR> &#060;TD&#062;&#060;%=qty%&#062;&#060;/TD&#062;<BR> &#060;TD&#062;&#060;%=FormatCurrency(price)%&#062; &#060;/TD&#062;<BR> &#060;TD&#062;&#060;%=FormatCurrency(cost)%&#062;& #060;/TD&#062;<BR> &#060;/TR&#062;<BR>&#060;%<BR> RS.MoveNext<BR>Loop<BR>RS.CLose<BR>%&#062;<BR>&#06 0;TR&#062;<BR> &#060;TD Colspan=3 align=right&#062;Total:&#060;/TD&#062;<BR> &#060;TD&#062;&#060;%=FormatCurrency(total)%&#062; &#060;/TD&#062;<BR>&#060;/TR&#062;<BR>&#060;/TABLE&#062;<BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•