Solved

adding numbers in a database

Posted on 2004-08-13
4
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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 100 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WSUS Client Issues 52 2,504
Help Fix A 403 error 13 121
PHP in Apache server 20 105
Problem to go to Web page 2 131
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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