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

Select Count Group By 1

Status
Not open for further replies.

A1Pat

IS-IT--Management
Jun 7, 2004
454
US
Hi all,

I'm trying to get data from my database with this code

Code:
mySQL = "SELECT Count(*) AS total_count FROM tblTechSupports " _
				&" WHERE DateTime = '" & get_day & "' " _
				&" AND charged = 1"

It works fine until I want to add another field into this code like:

Code:
mySQL = "SELECT Count(*),ID AS total_count FROM tblTechSupports " _
				&" WHERE DateTime = '" & get_day & "' " _
				&" AND charged = 1"

then, all bets are off.

An error keeps telling me to use GROUP BY or something likes that which I have very little understand about. Hope you can tell me how!!!

Thanks!
 
Whenever you use aggregates (COUNT, SUM, MIN, MAX) and select more than one column you have to group the data. So add
GROUP BY ID at the end of your query.

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Is this how it should look like:

Code:
"SELECT Count(*) AS total_count, ID FROM tblTechSupports " _
				&" WHERE DateTime = '" & get_day & "' " _
				&" AND charged = 1 GROUP BY ID"

I didn't get the error anymore, neither the data display.
by the way, this is how I want to display the ID data:
Code:
do while not rs.eof
  response.write rs("ID") & ","
rs.movenext
loop
 
Nevermind Paul,

I bad when I called the ID wrong (typo).

Thank you for the help.

By the way, what if I want to call more than just ID field. how about 3 or more fields. Do I need to use GROUP BY for all of them?
 
Yes,
But be carefull because it can change your result set as you add more columns.


- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Great... thanks alot!!!

keep up the good work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top