IIf statement including count and query aggregate error

Hello,

I am trying to run a query that includes an if statement with an aggregate function. The goal is to count the number of women in the table if the pwbox =2. If the pwbox is not 2, I want the query to return "0", not null.

When I execute the SQL statement below, I get the error message "you tried to execute a query that does not include the specific expression..."

How can I rewrite the query? I see that the problem is related to categorizing my IIf statement as an expression rather than in the group by clause, but I'm not sure how to fix it. I am not an advanced SQL user, so please keep that in mind.

SELECT TBADataForm.year, TBADataForm.term, IIf([pwbox]<>"2","0",Count([womanname])) AS [maternal death tba]
FROM TBADataForm
GROUP BY TBADataForm.year, TBADataForm.term, IIf([pwbox]<>"2","0",Count([womanname]));

JTT
ghaphisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasianCommented:
Is this what you wanted?

SELECT TBADataForm.year, TBADataForm.term, Count(IIf([pwbox]="2",[womanname])) AS [maternal death tba]
FROM TBADataForm
GROUP BY TBADataForm.year, TBADataForm.term
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k_murli_krishnaCommented:
Correction:

SELECT TBADataForm.year, TBADataForm.term, Count(IIf([pwbox]="2",[womanname],"0")) AS [maternal death tba]
FROM TBADataForm
GROUP BY TBADataForm.year, TBADataForm.term;

OR

SELECT TBADataForm.year, TBADataForm.term, Count(IIf([pwbox]<>"2","0",[womanname])) AS [maternal death tba]
FROM TBADataForm
GROUP BY TBADataForm.year, TBADataForm.term;

You can use single quotes instead of ". That is in SQL. In Java etc. it is treated as String and hence ".
0
ThomasianCommented:
k_murli_kris…,

Was the correction for my query? The 2nd parameter of the IIF function is optional. If it is not supplied, NULL is returned. And if you want to put an "else" value, you should use NULL instead of "0". COUNT function includes all non-null values in the count.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.