Asking for the impossible: query to return zero if no macth is found (1 Viewer)

cigarprofiler

Registered User.
Local time
Today, 04:55
Joined
Mar 25, 2017
Messages
32
On this thread, plog helped me out with a bunch of queries I was struggling with.

My question concerns the query Review_sub_10:

Code:
SELECT public_tblreview_items.review_id, Count(public_tblreview_items.review_item_id) AS observations_animal
FROM public_tblreview_items
WHERE (((public_tblreview_items.review_item_type_id)=16))
GROUP BY public_tblreview_items.review_id;

This query counts the number of observations in the "animal" category for each review_id. There are nine other categories, but not every review_id will have observations in all categories. And that's where I am asking for the impossible:

The normal behaviour for the query is not to return a record for a review_id for which there is no public_tblreview_items.review_item_type_id=16.

What I would like, however, is for the query to return zero in case Count(public_tblreview_items.review_item_id) finds nothing.

I have searched all morning for a possible solution. I checked out COALESCE, Nz, Iif-statements, EXIST, IN, adding Boolean logic to the query's WHERE-statement and what not, all to no avail. It's not a null-to-zero conversion, because there is no null - there is nothing.

I can think of a workaround in VBA where I enter a dummy observations with value 0 to each review, and rewrite the queries to sum review_item_type_value instead of counting review_item_id, but I'm hoping the query above can be adjusted to return zero in case of no observations.

Is that possible?
 
Last edited:

Ranman256

Well-known member
Local time
Today, 07:55
Joined
Apr 9, 2015
Messages
4,339
Run an OUTER join to count. Show ALL items for names,the nulls are
What is missing.
Ten just filter only the nulls. (Zeros)
 

isladogs

MVP / VIP
Local time
Today, 12:55
Joined
Jan 14, 2017
Messages
18,209
Even easier, use the unmatched query wizard
 

cigarprofiler

Registered User.
Local time
Today, 04:55
Joined
Mar 25, 2017
Messages
32
What I would like the query to return is (a) the count in case there is a match, and (b) a value of zero if there is no match - in the same query. I'm somewhat of an SQL/Access amateur, but if I interpret your answers correctly, they are only a way to get (b), at least that's what I found when I ran the unmatched query wizard just now.

Or am I doing it wrong?
 

isladogs

MVP / VIP
Local time
Today, 12:55
Joined
Jan 14, 2017
Messages
18,209
Sounds like a case of close but no cigar :D

One way of doing this is a UNION query which joins together 2 queries

The first part is the INNER JOIN query you started with to get the results with a count.
The second part is a modified version of the unmatched query but replacing the field with nulls with Nz(YourField,0)

Make sure both of these have the same number of fields in the same order

The UNION query can only be created in SQL view

So open the first query in SQL view & save as a new query
Open the second query in SQL view and copy to the clipboard
Return to the new query you saved in SQL view. At the end of this , after the ; type UNION then paste the second part of the query.

Save and run it.
Any problems, please include both queries in your reply
 

Users who are viewing this thread

Top Bottom