Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL SUM FUNCTION

Status
Not open for further replies.

AGNEW2PRG

Technical User
Aug 5, 2003
98
AE
Hi All,

I am trying to select muliple rows and sum the field "GBP" as a TOTAL Value.

The code below is what I have so far. Is this right? i am unable to output the total value of the GBP field..

Any assistance will be most appreciated.

Regards

<------- CODE ------------->

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("cos.mdb")
Set rsAddExpense = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT *,SUM(GBP) as TOTAL FROM TBLEXPENSES;"
Response.Write (Total)
 
Briefly, do not use * in your SELECT statement. If you want all of the fields, list them individually. Also, if you are going to use the SUM function, then you will also need to group everything else that you are returning in your recordset.

Otherwise, I'm not sure what your issue may be since you did not show an error message or otherwise indicate the problem (other than "unable to output the total value").

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Alternatively, if you want the SUM of EVERY record in the table, you don't need to use group by.

Select Sum(GBP) As Total from tblExpenses

If you want the sum of expenses for each 'something', then you need to use group by.

[tt][blue]
Select Name, Sum(GBP) As Total
From tblExpenses
Group By Name
[/blue][/tt]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top