?
Solved

adding numbers in a database

Posted on 2004-08-13
4
Medium Priority
?
146 Views
Last Modified: 2013-12-24
hello all,

I have a table with about 15+ records.
There is a number field and I want to add all the numbers together.  How can I do that?
Thanks.
0
Comment
Question by:Isaac
  • 2
4 Comments
 
LVL 5

Author Comment

by:Isaac
ID: 11798624
This is the code I have:

<CFQUERY NAME="view" datasource="A99">
      SELECT * FROM tblCount
</CFQUERY>

<cfloop query="view">
<cfset cnt = 0>
<CFSET cnt = cnt + #numb#>
</cfloop>
0
 
LVL 14

Accepted Solution

by:
Renante Entera earned 1600 total points
ID: 11798725
Actually, you can have it in this manner :

<CFQUERY NAME="view" datasource="A99">
  SELECT sum(number_field) as totalno
  FROM tblCount
</CFQUERY>

<cfoutput>#view.totalno#</cfoutput>

The result will give you the sum of all number within that table.

Regards!
eNTRANCE2002 :-)
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 11798743
Then by the way, just to correct your code.

You can have it in this way:

<cfparam name = "cnt" default = "0">

<CFQUERY NAME="view" datasource="A99">
  SELECT * FROM tblCount
</CFQUERY>

<cfloop query="view">
  <CFSET cnt = cnt + numb>
</cfloop>

<cfoutput>#cnt#</cfoutput>

Then, the result is still the same with the code above.

Best wishes!
eNTRANCE2002 :-)
0
 
LVL 6

Assisted Solution

by:reitzen
reitzen earned 400 total points
ID: 11798988
entrance2002 is absolutely correct in his comment.  There are usually several ways to write a section of code and still get the same results.  That being said, instead of rewriting your code, my comment is included to help you understand why your code doesn't work.  So, you can become more familiar with the CF language.

First, your query is okay.  However, for performance reasons, you want to stay away from using the wildcard "*".  Listing all of your field names will give you better performance (on tables with lots of columns) and you'll have a visible list of what fields your query will be returning.

Second, you will never get an accurate value in the "cnt" variable.  Why?  Because you are setting its value back to zero with every loop.  Move the line "<cfset cnt = 0>" above the <cfloop query="view"> and you'll see the results of the "cnt" variable returning the correct value.

Third, when you are referring to variables within a CF tag, you don't need to use the pound signs unless you are surrounding the variable in quotes (there are a couple of exceptions, but this is typically the rule).
Example:
Right  =  <cfset cnt = cnt + numb>
Wrong = <cfset cnt = cnt + #numb#>

Right = <cfset cnt = cnt + "#numb#">
Wrong = <cfset cnt = cnt + "numb">  (CF will look at "numb" as a string)

HTH
Rob
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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