DBQuery and SQL function COUNT not working

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
DingDong
Newbie
Posts: 15
Joined: Tue May 11, 2010 11:28 am
Location: Canada

DBQuery and SQL function COUNT not working

Post by DingDong » Thu May 13, 2010 9:41 am

Hello,

Database Table Clients has 4 records.
I run the following script and the return result is 1.
I would expect 4.
Is there a problem with the DBQuery function or the script?

Thankyou!
//Connect to Datasource
//
Let>str=Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\sample.mdb;Uid=Admin;Pwd=toto;
DBConnect>str,dbRep
//
//Perform SELECT query
//
Let>SQL=SELECT COUNT(ClientID) FROM Clients
DBQuery>dbRep,SQL,TabCLIENTS,NumRecs,NumFields
//
Message>%NumRecs%
//
//Close database connection
DBClose>dbRep

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu May 13, 2010 11:08 am

That's correct. You're looking at the number of records returned. Your SQL will return ONE row. So what your message box shows you is correct.

You want to look at the VALUE INSIDE the data returned. The data you want is in:

TabCLIENTS_1_1

The ONE row returned contains the count of ClientIDs.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

use count(*) to count null values as well

Post by adroege » Thu May 13, 2010 12:18 pm

There is another possibility....


That is DingDong expects the count to return 4 and it returns the number 1.

So.... the possibility exists that the column clientid contains null in 3 of the 4 rows. To have the count function give you the row count and ignore any nulls it encounters do the following:

count(*)


Use the asterisk instead of giving it a column name in the table.

DingDong
Newbie
Posts: 15
Joined: Tue May 11, 2010 11:28 am
Location: Canada

Post by DingDong » Thu May 13, 2010 12:29 pm

Thank you mtettmar and adroege for your reply.

By changing the statement to:

With the following

Code: Select all

Let>SQL=SELECT ClientID FROM Clients
The result show in the message is 4.

With the COUNT(*) function the result is still 1.

The ClientID field is an indexed field and null value is not permitted.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu May 13, 2010 12:51 pm

Of course. Your second SQL returns ALL records, so NumRecs will be the number of records returned.

Your first SQL returns **ONE** row, so NumRecs will return ONE. In your first SQL the count value is INSIDE the data returned. The SQL returns one field in one record, so TabCLIENTS_1_1 will hold the count.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

DingDong
Newbie
Posts: 15
Joined: Tue May 11, 2010 11:28 am
Location: Canada

Post by DingDong » Thu May 13, 2010 1:16 pm

Thank you mtettmar,

Could you tell me how will I retreive the data inside the row bring by the first statement and show it in a message.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu May 13, 2010 1:19 pm

MessageModal>TabCLIENTS_1_1
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

DingDong
Newbie
Posts: 15
Joined: Tue May 11, 2010 11:28 am
Location: Canada

Post by DingDong » Thu May 13, 2010 1:35 pm

mtettmar,

I understand now, thank you for your patience and your eplanations.

DingDong

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts