[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SUM of SQL statement

Posted on 2011-04-28
5
Medium Priority
?
389 Views
Last Modified: 2012-05-11
I am trying to create a SUM of the records queried from the database.

My original statement to get the individual records, which i am displaying with a repeat region is:

SELECT tblchurch.churchID, tblchurch.churchName, tblchurchinfo.churchInfoID, tblchurchinfo.churchAttendance, tblchurchinfo.churchSSAttendance, tblchurchinfo.churchResponsibility, tblchurchinfo.dateID, tblchurchinfo.churchID, tblmonths.monthID, tblmonths.monthLabel
FROM tblchurch, tblchurchinfo, tblmonths
WHERE tblchurchinfo.dateID = dateVar AND tblchurchinfo.churchID = tblchurch.churchID    AND tblchurchinfo.dateID = tblmonths.monthID AND tblchurchinfo.yearID = yearVar
ORDER BY tblchurch.churchName ASC

My SUM SQL statement is:

SELECT tblchurch.churchID, tblchurch.churchName, tblchurchinfo.churchInfoID, SUM(tblchurchinfo.churchAttendance, tblchurchinfo.churchSSAttendance, tblchurchinfo.churchResponsibility), tblchurchinfo.dateID, tblchurchinfo.churchID, tblmonths.monthID, tblmonths.monthLabel
FROM tblchurch, tblchurchinfo, tblmonths
WHERE tblchurchinfo.dateID = dateVar AND tblchurchinfo.churchID = tblchurch.churchID    AND tblchurchinfo.dateID = tblmonths.monthID AND tblchurchinfo.yearID = yearVar
ORDER BY tblchurch.churchName ASC

however, this is not working.

I am currently getting the data into a table repeating for all the info in the query.  It seems it would be just as simple to add the column totals from the repeat region that is created.  Is it possible to SUM a Do/While loop?
0
Comment
Question by:axessJosh
5 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 35484705
something like below?

If not please post some sample data and the expected result.
SELECT 	tblchurch.churchID, 
	tblchurch.churchName, 
	tblchurchinfo.churchInfoID, 
	SUM(tblchurchinfo.churchAttendance + tblchurchinfo.churchSSAttendance + tblchurchinfo.churchResponsibility), 
	tblchurchinfo.dateID, 
	tblchurchinfo.churchID, 
	tblmonths.monthID, 
	tblmonths.monthLabel

FROM tblchurch, tblchurchinfo, tblmonths
WHERE 	tblchurchinfo.dateID = dateVar AND 
	tblchurchinfo.churchID = tblchurch.churchID    AND 
	tblchurchinfo.dateID = tblmonths.monthID AND 
	tblchurchinfo.yearID = yearVar

group by tblchurch.churchID, 
	tblchurch.churchName, 
	tblchurchinfo.churchInfoID, 
	tblchurchinfo.dateID, 
	tblchurchinfo.churchID, 
	tblmonths.monthID, 
	tblmonths.monthLabel

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35484735
if your joins result in "duplicates", you need to either use DISTINCT or GROUP BY... but before doing the SUM.

so, you might need to subquery for the SUM ...
however, your second select seems wrong syntax?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35485918
What do you mean when you say it is "not working?"  What did you expect that you did not get?
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35487335
Actually, i've got it working locally, but when i put on my public server it gave me the error requiring a 'group by' clause.  Now, on my public server, it totals on the first record it finds, not all the records.

works fine locally.

here is my SQL:

$query_rsInfoSum1 = sprintf("SELECT SUM(tblchurchinfo.churchAttendance) as total, tblchurchinfo.dateID, tblchurchinfo.yearID FROM tblchurchinfo WHERE tblchurchinfo.dateID = %s AND  tblchurchinfo.yearID = %s GROUP BY tblchurchinfo.churchAttendance", GetSQLValueString($dateVar_rsInfoSum1, "int"),GetSQLValueString($yearVar_rsInfoSum1, "int"));
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 35487351
check this .
$query_rsInfoSum1 = sprintf("SELECT SUM(tblchurchinfo.churchAttendance) as total, tblchurchinfo.dateID, tblchurchinfo.yearID FROM tblchurchinfo WHERE tblchurchinfo.dateID = %s AND  tblchurchinfo.yearID = %s GROUP BY tblchurchinfo.dateID, tblchurchinfo.yearID", GetSQLValueString($dateVar_rsInfoSum1, "int"),GetSQLValueString($yearVar_rsInfoSum1, "int"));

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 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