Print Page | Close Window

SUM help

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=13968
Printed Date: 30 March 2026 at 3:03pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: SUM help
Posted By: ub3rl337ch3ch
Subject: SUM help
Date Posted: 23 February 2005 at 5:38pm
I'm trying to add the sums of two fields in a database, divide one of the fields by the result, and then write both results to a table. Here's what I have:
 
sql = "SELECT SUM(AwayPoint) AS totalaway FROM Score WHERE AwayID =" & vrec("CLId")
vrec2.Open sql, adoCon
sql = "SELECT SUM(HomePoint) AS totalhome FROM Score WHERE HomeID =" & vrec("CLId")
vrec1.Open sql, adoCon dim totalp, percent, totalh, totala
totalh = vrec1("totalhome")
totala = vrec2("totalaway")
totalp = totalh + totala
percent = totalh/totalp
 
and then a response.write for totalp and precent
 
It doesn't give me any errors, but it does display a grand total of zippo in the table. Could someone please point out where i've gone wrong?
<%Response.write vrec("TName")%>
<%Response.Write totalp%>
<%Response.write percent%>



Replies:
Posted By: dj air
Date Posted: 24 February 2005 at 6:11am
what happends when you run the code.?

have you tried CLng() the reord values


Posted By: ub3rl337ch3ch
Date Posted: 27 February 2005 at 6:31pm
cheers, CLng() worked a treat.


Posted By: ub3rl337ch3ch
Date Posted: 27 February 2005 at 7:06pm
ok, now I'm getting an Overflow error (just one word, overflow) on the line near the bottom where I try to divide. here's my updated script
 
sql = "SELECT SUM(AwayPoint) AS totalaway FROM Score WHERE AwayID =" & vrec("CLId")
vrec2.Open sql, adoCon
sql = "SELECT SUM(HomePoint) AS totalhome FROM Score WHERE HomeID =" & vrec("CLId")
vrec1.Open sql, adoCon
totalh = vrec1("totalhome")
if IsNull(totalh) then totalh = 0 end if
totalh = CLng(totalh)
totala = vrec2("totalaway")
if IsNull(totala) then totala = 0 end if
totala = CLng(totala)
totalp = totalh + totala
vrec1.close
vrec2.Close
sql = "SELECT SUM(HomePoint) AS againsthome FROM Score WHERE AwayID =" & vrec("CLId")
vrec2.Open sql, adoCon
sql = "SELECT SUM(AwayPoint) AS againstaway FROM Score WHERE HomeID =" & vrec("CLId")
vrec1.Open sql, adoCon
againsth = vrec2("againsthome")
if IsNull(againsth) then againsth = 0 end if
againsth = CLng(totalh)
againsta = vrec1("againstaway")
if IsNull(againsta) then againsta = 0 end if
againsta = CLng(againsta)
againstp = againsth + againsta
againstp = CLng(againstp)
percent = totalh/againstp <-- overflow error here
percent = CLng(percent)


Posted By: dj air
Date Posted: 27 February 2005 at 7:10pm
you dont need to use CLng() on all places its used if you do it when its bought into the application/ out of the database it will be done.


Posted By: dj air
Date Posted: 27 February 2005 at 7:11pm
also may be an idea to put the calulations witin brackets as it makes sure you dont get some errors im not sure about the over flow..

the number is becoming to big i think it means


Posted By: dpyers
Date Posted: 27 February 2005 at 7:29pm
divide by 0 will also cause an overflow

-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: ub3rl337ch3ch
Date Posted: 27 February 2005 at 8:36pm
thanks, div by 0 was the problem... just had no idea what overflow was about. Next problem has cropped up, in that the division seems to round automatically, so that 2/6 = 0, 20/6 = 3, 200/6 = 33, 2000/6 = 333. I've tried format number on both the quotient and the numer/denominator, to no avail. If i try to divide by 10 or 100 later to move the decimal place, it just rounds again. How can i get the script to stop rounding automatically?



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net