Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Count records in query
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Count records in query

 Post Reply Post Reply
Author
gr8indianbear View Drop Down
Groupie
Groupie


Joined: 07 November 2002
Location: United States
Status: Offline
Points: 64
Post Options Post Options   Thanks (0) Thanks(0)   Quote gr8indianbear Quote  Post ReplyReply Direct Link To This Post Topic: Count records in query
    Posted: 10 June 2004 at 9:39pm

i want to count the number of records in a query without having to run a seperate query. i think i've jsut simply fogotten how to do this. this is the SQL string i had:

SELECT Count(ID) AS gf_Count, ID, Title, Blurb, Author, gf_Cat FROM gfaith WHERE (((Body) Like '%God%')) ORDER BY gf_Cat, Beginning;

the page came up with this error

Microsoft JET Database Engine error '80040e21'

You tried to execute a query that does not include the specified expression 'ID' as part of an aggregate function.

/search.asp, line 215

Line 215 is where the recordset is opened and the SQL Query is run.

any ideas?

Back to Top
Phat View Drop Down
Senior Member
Senior Member


Joined: 23 February 2003
Status: Offline
Points: 386
Post Options Post Options   Thanks (0) Thanks(0)   Quote Phat Quote  Post ReplyReply Direct Link To This Post Posted: 10 June 2004 at 9:46pm
Think you will need the table name before each field.

SELECT Count(ID) AS gf_Count, ID, gfaith.Title, gfaith.Blurb, gfaith.Author, gfaith.gf_Cat FROM gfaith WHERE (((Body) Like '%God%')) ORDER BY gfaith.gf_Cat, gfaith.Beginning;
Back to Top
gr8indianbear View Drop Down
Groupie
Groupie


Joined: 07 November 2002
Location: United States
Status: Offline
Points: 64
Post Options Post Options   Thanks (0) Thanks(0)   Quote gr8indianbear Quote  Post ReplyReply Direct Link To This Post Posted: 10 June 2004 at 10:00pm

still nothing. i even tried running the query in Access changing the wildcards to '*'. still got the same error message, that

You tried to execute a query that does not include the specified expression 'ID' as part of an aggregate function.

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 10 June 2004 at 10:07pm
No, the problem is that you are using an aggregate function 'Count' and do not use aggregate function, or group by for the rest of the fields. Theoretically the following query would work
SELECT Count(gf_Cat.ID) AS gf_Count, gf_Cat.Title, gf_Cat.Blurb, gf_Cat.Authorgf_Cat FROM gf_Cat GROUP BY gf_Cat.Title, gf_Cat.Blurb, gf_Cat.Authorgf_Cat, gf_Cat.Body HAVING (((gf_Cat.Body) Like '%god%'));

thus if a title, blurb or author are different they get their own row and so are not counted as id. Basically saying the query, the way you have it makes not much sense, unless you try to accomplish something different, for example just counting all the records in the first column, no matter what's the criteria, you could use something like
SELECT (Select Count(id) from gf_Cat) AS gf_Count, gf_Cat.Title, gf_Cat.Blurb, gf_Cat.Authorgf_Cat FROM gf_Cat WHERE (((gf_Cat.Body) Like '%god%'));
Back to Top
gr8indianbear View Drop Down
Groupie
Groupie


Joined: 07 November 2002
Location: United States
Status: Offline
Points: 64
Post Options Post Options   Thanks (0) Thanks(0)   Quote gr8indianbear Quote  Post ReplyReply Direct Link To This Post Posted: 10 June 2004 at 11:26pm

ok, i got it changing what michael had from

SELECT (Select Count(id) from gf_Cat) AS gf_Count, gf_Cat.Title, gf_Cat.Blurb, gf_Cat.Authorgf_Cat FROM gf_Cat WHERE (((gf_Cat.Body) Like '%god%'));

to

SELECT (Select Count(id) from gfaith Where (((gfaith.Body) Like '*god*'))) AS gf_Count, gfaith.Title, gfaith.Blurb, gfaith.Author, gfaith.gf_Cat
FROM gfaith
WHERE (((gfaith.Body) Like '*god*'));

the table name was gfatih, and gf_Cat is a field. i made it work to my purposes by nesting a second query into the frist query.

i would have used RecordCount, but that only returned a result of -1 which means it was unable to count the recordset. but this method works

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 11 June 2004 at 8:59am
Realize that subqueries in Access pay a high penalty in performance.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 11 June 2004 at 12:28pm

As michael noted, nested/sub queries can blow your performance, but the extent depends upon your db structure.

You may want to use a couple of timers to check the performance differences between the above query, and running a separate query and just checking the rs.RecordCount property.

Also, the type of rs you create will impact performance. In general, a forward_only rs will be faster to create, but should be suitable for getting a record count.


Lead me not into temptation... I know the short cut, follow me.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.