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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Count(*)

Status
Not open for further replies.

JohannIcon

Programmer
Sep 3, 2002
440
MT
I know this is a silly question but I forgot how to use Select Count(*), its been quite a while now.

I am using it like this:-

sqlCount="SELECT COUNT(*) AS TotCount FROM pollvoters WHERE pollID=" & pollid
set rsCount=cnn.execute(sqlCount)
response.Write(TotCount)

But nothing is displayed on the screen. At the moment I have no entries in the database, but isn't it supposed to return 0?
Thanks for your help and time
 
Drop the "AS TotCount" from the SQL statement. The result set will contain the number of records.


--------------------------------
If it ain't broke, don't fix it!
 
should the last line not be...

response.write rsCount("TotCount")?

Nick
 
Another problem please, I wish to make the variable dynamic so that I can get all the values. What i have done is like this:-

rs1.MoveFirst
n=1
do
PollAnsID = rs1("PollAnsID")
PollAnsText = rs1("PollAnsText")

'get the count of all the votes for this pollID per AnserID
sqlCount="SELECT COUNT(*) AS TotVotes(n) FROM pollvoters WHERE pollID=" & pollid
set rsCount=cnn.execute(sqlCount)
TotVotes(n) = (rsCount("TotVotes(n)"))
response.Write(PollAnsText & " has got " & TotVotes(n))
n = n+1

rs1.MoveNext
if rs1.eof then
exit do
End If
loop until rs1.eof

But I have an error in the SQL. How can I arrange that?

Thanks for your help!
 
The error is due to the fact you are using "totvotes(n)" as the column name in the resultset. You can't use parentheses like that.

What is the table structure? I think you can probably return each poll with the count in one SQL statement (using a GROUP BY clause) rather than loop through for each poll.

--James
 
My table structure is like this:-

pollID,pollAnsID,userID.

Each time the user votes, I add an entry with his userid, id of the question and id of the answer.

Thanks
 
OK, to get the total number of votes for each question use:

Code:
SELECT pollid, COUNT(*) AS votes
FROM table
GROUP BY pollid
ORDER BY pollid

To get number of votes for each answer for a particular question use:

Code:
SELECT pollansid, COUNT(*) AS votes
FROM table
WHERE pollid = 123
GROUP BY pollansid
ORDER BY pollansid

Also, to get a full list of total votes for each answer to each question use:

Code:
SELECT pollid, pollansid, COUNT(*) AS votes
FROM table
GROUP BY pollid, pollansid
ORDER BY pollid, pollansid

--James
 
Thanks James, I did this now:-

rs1.MoveFirst
n=1
do
PollAnsID = rs1("PollAnsID")
PollAnsText = rs1("PollAnsText")

'get the count of all the votes for this pollID per AnserID
sqlCount="SELECT pollansid, COUNT(*) AS votes FROM pollVoters WHERE pollid =" & pollid & " GROUP BY pollansid ORDER BY pollansid"
set rsCount=cnn.execute(sqlCount)
Votes = (rsCount("Votes"))
response.Write(PollAnsText & " has got " & Votes)
n = n+1

rs1.MoveNext
if rs1.eof then
exit do
End If
loop until rs1.eof

However since Votes =1, it is displaying 1 for all the questions. Am I displaying them the wrong way?
 
What does rs1 contain (maybe post the SQL statement)? What value does pollid contain? Basically, what are you trying to do with this code?

--James
 
Ok here is my code:-

'*** Insert the answer into the pollvoters table
sql="insert into pollvoters(pollID,pollAnsID,userid)"
sql = sql & " values(" & pollid & "," & PollChosen & "," & userid & ")"
set rs=cnn.execute(sql)

'calculation of percentages:-
'get the count of all the votes for this pollID
sqlCount="SELECT COUNT(*) AS TotCount FROM pollvoters WHERE pollID=" & pollid
set rsCount=cnn.execute(sqlCount)

'now get all the the pollAnswers from the database for this question
Set rs1=Server.CreateObject("ADODB.Recordset")
rs1.ActiveConnection = cnn

sql1 = "SELECT * from pollAnswers where PollID=" & PollID
sql1=sql1 & " ORDER BY pollAnsID ASC"
'response.Write(sql1)
rs1.Open sql1, cnn, adOpenStatic, adLockReadOnly, adCmdText

'loop through all the recordset and record the amount of votes there are in the pollVoters for that particular AnsID and PollID
rs1.MoveFirst
n=1
do
PollAnsID = rs1("PollAnsID")
PollAnsText = rs1("PollAnsText")

'get the count of all the votes for this pollID per AnserID
sqlCount="SELECT pollansid, COUNT(*) AS votes FROM pollVoters WHERE pollid =" & pollid & " GROUP BY pollansid ORDER BY pollansid"
set rsCount=cnn.execute(sqlCount)
Votes = (rsCount("Votes"))
response.Write(PollAnsText & " has got " & Votes)
n = n+1

rs1.MoveNext
if rs1.eof then
exit do
End If
loop until rs1.eof
 
Ok I solved it James. I did a search in the pollVoters table to see if the pollAnsID exists in it. If it does not, the Vote is automatically 0. If it does exist, get the count. Here is the code I added:-

'check wether the pollAnsID is in the pollVoters table first
sqlAnsID="SELECT * FROM pollvoters WHERE pollAnsID=" & pollAnsID
set rsAnsID=cnn.execute(sqlAnsID)

if NOT rsAnsID.EOF or NOT rsAnsID.BOF then
'get the count of all the votes for this pollID per AnserID
sqlCount="SELECT pollansid, COUNT(*) AS votes FROM pollVoters WHERE pollid =" & pollid & " AND pollAnsID =" & pollAnsID & " GROUP BY pollansid ORDER BY pollansid"
set rsCount=cnn.execute(sqlCount)
Votes = (rsCount("Votes"))
response.Write(PollAnsText & " has got " & Votes)
else
response.Write(PollAnsText & " has got 0 Votes")
n = n+1
end if
 
Hmm.. you've basically coded a standard SQL join using VBScript! I think this should get what you want, plus only needs a single trip to the DB:

Code:
'do insert of vote here (as you have it)

'now get all the the pollAnswers from the database for this question
strSQL = "SELECT a.pollanstext, COUNT(*) AS votes " &_
  "FROM pollvoters v JOIN pollanswers a ON v.pollansid = a.pollansid " &_
  "WHERE v.pollid = " & pollid & " " &_
  "GROUP BY a.pollanstext ORDER BY a.pollanstext"

Set rsCount = cnn.Execute(strSQL)

Do Until rsCount.EOF
  Response.Write rsCount(&quot;pollanstext&quot;) & &quot; has got &quot; & rsCount(&quot;votes&quot;) & &quot; votes<br />&quot;
  rsCount.MoveNext
Loop

--James
 
Not nearly as efficient as it could be (see my example). A general rule is to try and keep database hits to a minimum. With your original method you were making a seperate hit and query for each answer for the question. Imagine if you started getting 10+ possible answers!

My example makes one query and returns a simple resultset containing a row for each question and the number of hits. It is more efficient and a lot quicker.

--James
 
James I got the following error from your query

Syntax error in FROM clause.

strSQL = &quot;SELECT a.pollanstext, COUNT(*) AS votes &quot; &_
&quot;FROM pollvoters v JOIN pollanswers a ON v.pollansid = a.pollansid &quot; &_
&quot;WHERE v.pollid = &quot; & pollid & &quot; &quot; &_
&quot;GROUP BY a.pollanstext ORDER BY a.pollanstext&quot;
 
Try adding an AS:

Code:
...FROM pollvoters AS v JOIN pollanswers AS a ON v.pollansid = a.pollansid...

If not, which DBMS are you using?

--James
 
Sorry, you need to specify the INNER join in Access:

Code:
...FROM pollvoters v INNER JOIN pollanswers a ON v.pollansid = a.pollansid...

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top