Got one more problem. I've got different sums to get an average value of.
For example i've got an article that's called article1 and another that's
called article2. Now here's the problem. If I use Count(*) or
Count(whatever), it wont give me the right result. Cause it counts both
fields where article is either article1 or article2. How do I solve this? I
just want it to divide by the number of votes for article1 and article2
respectively. Please help! =)
--
Martin Johansson
-----Ursprungligt meddelande-----
Från: Kyle Burns [mailto:kburns@c...]
Skickat: den 25 september 2001 19:37
Till: ASP Databases
Ämne: [asp_databases] RE: SV: RE: Vote System
Even better than Count(*), which will also give a performance hit, is to use
the Count function on a field that you know will contain a value (nulls are
skipped in Count) such as CodeID. I also noticed that you are referencing
the value of the field "Average" after the recordset object is closed and
set to Nothing. You'll need to modify the code to save this value to a
variable before closing the recordset. I kind of shy away from using words
that could ever conceivably be a keyword when I write my SQL statements. My
suggestion would be to have your SQL statement end up something like this:
"SELECT SUM(rating) / COUNT(codeid) AS avgRating FROM tblRating WHERE codeid
= " & choice
=================================
Kyle M. Burns, MCSD
ECommerce Technology Manager
Centra Credit Union
kburns@c...
-----Original Message-----
From: SD-Studios [mailto:info@s...]
Sent: Tuesday, September 25, 2001 11:21 AM
To: ASP Databases
Subject: [asp_databases] SV: RE: Vote System
Okay! Thanks! =)
--
Martin Johansson
-----Ursprungligt meddelande-----
Från: David Cameron [mailto:dcameron@i...]
Skickat: den 25 september 2001 01:30
Till: ASP Databases
Ämne: [asp_databases] RE: Vote System
Whoa there. There are faster ways of doing that.
First comment, never use recordcount. There is a page about it on Ken's site
(www.adopenstatic.com), but basically to get a true result you need to move
last and move first, which is a performance loss. COUNT(*) in the SQL
statement is always a better option.
<%
dim averagevote
Set rsd2 = Server.CreateObject("ADODB.Recordset")
strSQL= "SELECT * FROM tblRating WHERE CodeID = " & choice
strSQL = "SELECT SUM(rating)/COUNT(*) as Average " & _
"FROM tblRating WHERE CodeID = " & choice
rsd2.Open strSQL, conn, adOpenForwardOnly, adLockReadOnly
rsd2.close
Set rsd2 = Nothing
averagevote = int(rsd2("Average"))
If averagevote < 1 Then averagevote = 1
%>
<img src="images/<%=averagevote%>stars.gif" width="62"
height="12">
I checked the SQL string for SQL Server, it should work in Access. If not
you can return the sum and 2 different fields for Count and Sum and do the
calculation in the page.
regards
David Cameron
nOw.b2b
dcameron@i...
-----Original Message-----
From: Sam Clohesy [mailto:sam@e...]
Sent: Tuesday, 25 September 2001 1:06 AM
To: ASP Databases
Subject: [asp_databases] RE: Vote System
Something like this:
<%
dim totalvote
dim votecount
dim averagevote
Set rsd2 = Server.CreateObject("ADODB.Recordset")
strSQL= "SELECT * FROM tblRating WHERE CodeID = " & choice
rsd2.Open strSQL, conn, 1'adOpenForwardOnly
votecount = rsd2.recordcount
do while NOT rsd2.EOF
totalvote = totalvote + rsd2("rating")
rsd2.movenext
loop
if votecount > 0 then
averagevote = int(totalvote/votecount)+1
end if
%>
<img src="images/<%=averagevote%>stars.gif" width="62"
height="12">
No doubt better way...
Thanks
Sam