473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Access - count(iif based on values in 2 fields?

I’m having trouble with a report field.

Need it to count records in a table only where BOTH:
1. Field1 – date is less than 1/1/2013; and
2. Field2 – is null

In the table, Fields 1 and 2 are date fields and either contain a date, or are left empty.

I’ve tried the following:

Expression: Count(IIf(([tbl].[field1]<#1/01/2013#) And ([tbl].[field2] Is Null),1,0))

but... not working. Any help would be appreciated!
Jun 17 '14 #1
12 10671
jimatqsi
1,271 Expert 1GB
Your expression says <#1/01/2013# but your description of the problem says IS 1/1/2013. Is the code wrong or the description?

Jim
Jun 17 '14 #2
The description is wrong - will edit now
Jun 17 '14 #3
Rabbit
12,516 Expert Mod 8TB
Count doesn't care what it counts. Put your criteria in the where clause instead of using iif.
Jun 17 '14 #4
Rabbit, I initially tried to put the criteria in the where clause but Access error message "cannot have aggregate function in where clause" arose.

I did some google searching and came across people using count(iif but none where the iif includes 2 variables (as above).
Jun 17 '14 #5
twinnyfo
3,653 Expert Mod 2GB
shooshed,

Have you tried using the DCount Function:

Expand|Select|Wrap|Line Numbers
  1. DCount("*", "tbl", "[Field1] < #1/1/2013# AND [Field2] Is Null)
This has works with my tables....

Hope this hepps!
Jun 17 '14 #6
Rabbit
12,516 Expert Mod 8TB
You don't put the aggregate function in where clause. You only put the filter criteria in the where clause. You leave the aggregate function in the select.

Those examples you saw using count(iif()) are wrong. It doesn't work. If anything, you saw sum(iif()). But while that technically works, it is very inefficient if you don't need to return all the rows. It's better to use the algorithm I laid out in the first paragraph.
Jun 17 '14 #7
NeoPa
32,556 Expert Mod 16PB
@Twinny.
Domain Aggregate functions (DCount()) would be relatively inefficient as they have to enquire of the data separately, whereas Aggregate functions (Count(); Sum(); etc) perform within the context of the existing query so are more efficient. Also bear in mind that any use of a Domain Aggregate function would also need to reflect the current grouping in the filter. Your posted suggestion would count all of the records in the whole table rather than just those appropriate within the current GROUP BY group.

@Rabbit.
Rabbit:
Those examples you saw using count(iif()) are wrong. It doesn't work.
That way they won't, but they can be used if the zero value is replaced with Null.

EG :
Expand|Select|Wrap|Line Numbers
  1. Expression: Count(IIf(([tbl].[field1]<#1/1/2013#) And ([tbl].[field2] Is Null),1,Null))
Of course, the Sum() version will work and may be easier to grasp :
Expand|Select|Wrap|Line Numbers
  1. Expression: Sum(IIf(([tbl].[field1]<#1/1/2013#) And ([tbl].[field2] Is Null),1,0))
Jun 29 '14 #8
twinnyfo
3,653 Expert Mod 2GB
@NeoPa,

This may be slightly off-topic, but I think it is related. You said:

Domain Aggregate functions (DCount()) would be relatively inefficient as they have to enquire of the data separately, whereas Aggregate functions (Count(); Sum(); etc) perform within the context of the existing query so are more efficient. Also bear in mind that any use of a Domain Aggregate function would also need to reflect the current grouping in the filter. Your posted suggestion would count all of the records in the whole table rather than just those appropriate within the current GROUP BY group.
Please explain, so that I understand, because it appears that the DCount, Sum and Count all appear (in this case) to look at all records and evaluate according to the same criteria.

The reason I ask is obviously because I wish to have a more efficient DB, and I use DCount numerous times in finding the number of records that meet certain criteria. If I have better, more efficient ways to do this, then, obviously I wish to do that. However, I also wish to understand why one way is more efficient, when, on the surface, they appear identical but use different language.

Thanks!
Jul 2 '14 #9
Rabbit
12,516 Expert Mod 8TB
Domain aggregate functions such as DCount and DSum have extra overhead because they call code for each row (assuming the function call references a field in the recordset).

Whereas Count() and Sum() work with the set of data just the one time.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT country, count(*)
  2. FROM tblPopulation
  3. GROUP BY country
Expand|Select|Wrap|Line Numbers
  1. SELECT country, DCOUNT("*", "tblPopulation", "country = '" country & "'")
  2. FROM tblPopulation
  3. GROUP BY country
Assuming there are 10 distinct countries, the DCount version calls the code 10 times. Each time it does a separate count of the data. Basically, it's running 10 separate queries to provide the counts.

The Count version pulls the data once and does the counts.

However, if the domain aggregate function does not reference a field in the recordset, the overhead is negligible because the optimizer runs the function once.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT a.country, b.allCount
  2. FROM tblPopulation a, (
  3.    SELECT COUNT(*) AS allCount
  4.    FROM tblPopulation
  5. ) b
Expand|Select|Wrap|Line Numbers
  1. SELECT a.country, DCount("*", "tblPopulation")
  2. FROM tblPopulation
There will be almost no performance difference between the first version and the second. However, I still recommend the first version because of consistency and portability. The first version can be ported more easily if the database is moved to another DBMS.
Jul 2 '14 #10
twinnyfo
3,653 Expert Mod 2GB
Rabbit,

As usual, you explained well to a young Jedi. Some day I may fully understand all the secrets of the dark side of the Force.

Thanks for the excellent explanation!
Jul 2 '14 #11
Rabbit
12,516 Expert Mod 8TB
No problem twinny, glad to be of help.
Jul 2 '14 #12
NeoPa
32,556 Expert Mod 16PB
Rabbit has provided illustrations that range from the most, to the absolute least, effect of using Domain Aggregate instead of Aggregate functions, and rightly indicates that even in such a rare case as when aggregating across the full domain for a domain which isn't otherwised reference within the SQL, it is still preferable to use the latter instead of the former.

His first example illustrates that the Domain Aggregate function is not equivalent to an Aggregate one where a GROUP BY is used (He has criteria in the DCount() that specifies which group of data within the domain to process through). Even if no GROUP BY is used (IE A single output record is required) then DCount() still ensures the domain is processed through twice instead of only once, and because it comes via a completely separate function call it cannot be included in any optimisations determined by pre-processing the SQL (Such as which indexes to use etc).

PS Twinnyfo - we prefer to think of ourselves as on the light side of The Force if you don't mind ;-) We'll leave the secrets of the dark side to others.
Jul 3 '14 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Les Juby | last post by:
Getting really stumped here..... Using Classic.ASP and after an editing page has been completed I need to write the values back to an Access 2000 database. But I'm battling with a checkbox...
4
by: Bob Rock | last post by:
Hello, I'd like to programmatically access keys and values of on .resources file embedded in my assembly. I've seen that the assembly class provides the GetManifestResourceStream method .......
5
by: sklett | last post by:
I'm not real experienced with asp.net so this may be obvious. I've got a situation where some of my client side javascript is causing my posted form's controls to lose their values. In other...
5
by: joshua.nicholes | last post by:
I have an access database that consists of two tables.A data collection table and a species list table. The data collection table has about 1500 records in it and the species list has about 600....
0
by: notmoonlighting | last post by:
I am new to Access 2007, a former FileMaker Pro database user. I am trying to replicate some functionality that was available in the FM DB, which involved Calculated Fields in one of the Tables. ...
0
by: John James | last post by:
I need to create a new Access DB based on one already existing...Hence, I am attempting to copy the db struture and not the data; It's a yearly db I need to track/create. I looked on this site and...
1
by: Prabu Mani | last post by:
Hi Am novice to Access DB querying My scenario is ColumnA ColumnB A 1 Already Inserted A 1 Should not allow as the same
4
by: Silver993 | last post by:
Hi, Is there any Add on or Code that I can add to my Access 2003 based Application with Switchboard. I want the user to be able to backup and restore the application at will. Should that backup...
11
by: kadishzm | last post by:
Hi Everyone, I have recently been tasked with taking over a bunch of Access databases that I didn't create. My VBA skills are weak to say the best but I have a particular problem I am working on....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.