MS ACCESS 2016, Count IIF issue

Pettor

Board Regular
Joined
Aug 8, 2015
Messages
175
Hi All,

I have a table with two columns like the below and I want to write a query that will count the YES values grouping by the A column. Any suggestions?
I tried Count(IIf()) and DCOUNT functions but none of them worked...


1
YES
2 YES
3 YES
3
4 YES
5 NO
5
YES
5 YES

<colgroup><col style="width:48pt" width="64" span="2"> </colgroup><tbody>
</tbody>



11
21
31
41
52

<colgroup><col style="width:48pt" width="64" span="2"> </colgroup><tbody>
</tbody>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Something like:

SELECT Table1.x, Sum(IIf([y]="yes",1,0)) AS ycount
FROM Table1
GROUP BY Table1.x;

where x and y are the relevant fields with x consisting of numbers and y = yes, no, or null.
 
Upvote 0
As it is still not working can you please find the mistake here?

SELECT [2P_TABLE].[ONE], Sum(IIf([2P_TABLE].[TWO]="YES",1,0)) AS ycount
FROM [2P_TABLE]
GROUP BY [2P_TABLE].[ONE];

I receive the message: Data type mismatch in criteria expression. I don't know why.
 
Upvote 0
Should work:
Code:
SELECT
    [Field1], 
    Count([Field1]) as Total
FROM 
    TABLE1
WHERE 
    [Field2] = 'YES'
GROUP BY [Field1]

But probably would have no records if there were only 'No' in any case (or no Yesses at all, anyhow)
 
Upvote 0
The query you posted works for me, BTW. Are sure these are text fields with text values for "YES" and "NO"?
 
Upvote 0
Make sure your test data is complete with all the types of results you want to see.
 
Upvote 0
The query you posted works for me, BTW. Are sure these are text fields with text values for "YES" and "NO"?

Again, check the data types...
 
Upvote 0
The data type is a YES/NO field with a default value of "No". Isn't it correct?
When I tick the box it gets the "Yes" value and if not it gets the "No".
Am I missing something?
 
Upvote 0
A Yes/No field is not text.
Yes = -1
No = 0

so you can just sum then I guess:
Code:
SELECT 
	[2P_TABLE].[ONE], 
	SUM(-[2P_TABLE].[TWO]) AS ycount
FROM [2P_TABLE]
GROUP BY [2P_TABLE].[ONE];

Npte: Are you sure there is a default value of "No" (zero). Your test data shows a blank!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,710
Messages
6,161,445
Members
451,706
Latest member
SMB1982

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top