Display if count = 5

W

WCDoan

I have a query that counts the number of animals a person has. I would like
to only display those where count is equal to 5. I tried putting = 5 on the
criteria line of the field that is being counted, but that doesn't do
anything. Tried putting Count = 5 in the criteria line of the field being
counted, but that gets a 'data mismatch' error. How and where would I do this
in the query?
Thanks,
RandyM
 
W

WCDoan

Thanks for replying Douglas. I wasn't in the office this weekend, so I've
just now seen your reply. The following is the SQL..is that what you want?
This doesn't have the 'checking for 5' in it. Just the basic counting which
works. I just don't know how to check for a count of 5 and only display those
records.
Thanks again,
RandyM

SELECT DISTINCTROW Count(tblAnimals.AnimalID) AS CountOfAnimalID,
tblAnimals.OwnerID_AnimalTbl, tblOwners.OwnerID, tblOwners.First,
tblOwners.Last
FROM tblOwners INNER JOIN tblAnimals ON tblOwners.OwnerID =
tblAnimals.OwnerID_AnimalTbl
GROUP BY tblAnimals.OwnerID_AnimalTbl, tblOwners.OwnerID, tblOwners.First,
tblOwners.Last, tblAnimals.AnimalDeceased, tblAnimals.AnimalMoved
HAVING (((tblAnimals.AnimalDeceased)=False)) OR
(((tblAnimals.AnimalMoved)=False));
 
G

Gary Walter

try this:

SELECT
Count(tblAnimals.AnimalID) AS CountOfAnimalID,
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last
FROM
tblOwners
INNER JOIN
tblAnimals
ON tblOwners.OwnerID = tblAnimals.OwnerID_AnimalTbl
GROUP BY
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last,
tblAnimals.AnimalDeceased,
tblAnimals.AnimalMoved
HAVING
(tblAnimals.AnimalDeceased=False
OR
tblAnimals.AnimalMoved=False)
AND
(Count(*)=5);
 
W

WCDoan

Thanks Gary, that did it. I appreciate the help. Could you explain it? I'm
working my way thru Access and while I've learned a great deal from the
answers that the people here have posted to mine and others questions
sometimes I don't quite understand why something is. What is the (*) in the
last Count? And, if you count something in the design grid in a query, you
can then use the word Count as a field to refer back to in the query like
this did? I don't mean to be presumptious in asking these questions, so if
you don't want to answer that's fine. But, thank you so much for making this
work!
RandyM
 
G

Gary Walter

I have a query that counts the number of animals a person has. I would like
to only display those where count is equal to 5. I tried putting = 5 on the
criteria line of the field that is being counted, but that doesn't do
anything. Tried putting Count = 5 in the criteria line of the field being
counted, but that gets a 'data mismatch' error. How and where would I do
this
in the query?
///////////////////
The following is the SQL..is that what you want?
This doesn't have the 'checking for 5' in it. Just the basic counting which
works. I just don't know how to check for a count of 5 and only display
those
records.

SELECT
DISTINCTROW
Count(tblAnimals.AnimalID) AS CountOfAnimalID,
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last
FROM
tblOwners
INNER JOIN
tblAnimals
ON
tblOwners.OwnerID = tblAnimals.OwnerID_AnimalTbl
GROUP BY
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last,
tblAnimals.AnimalDeceased,
tblAnimals.AnimalMoved
HAVING
tblAnimals.AnimalDeceased=False
OR
tblAnimals.AnimalMoved=False
////////////////////////////////////
try this:

SELECT
Count(tblAnimals.AnimalID) AS CountOfAnimalID,
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last
FROM
tblOwners
INNER JOIN
tblAnimals
ON tblOwners.OwnerID = tblAnimals.OwnerID_AnimalTbl
GROUP BY
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last,
tblAnimals.AnimalDeceased,
tblAnimals.AnimalMoved
HAVING
(tblAnimals.AnimalDeceased=False
OR
tblAnimals.AnimalMoved=False)
AND
(Count(*)=5);
///////////////////////////////////////
....that did it....
What is the (*) in the last Count?
And, if you count something in the design grid in a query,
you can then use the word Count as a field to refer back to
in the query like this did?
//////////////////////////////////////////

Hi Randy,

I don't believe your problem had anything
to do with "Count(*) =5" vs "Count(AnimalID)=5"

I suspect you had something like the following
grid when it "didn't do anything."

Field: AnimalID AnimalDeceased AnimalMoved
Table: tblAnimals tblAnimals tblAnimals
Total: Count Group By Group By
Sort:
Show:
Criteria: 5 False
or: False
or:

Setting multiple criteria on the grid is not an easy thing
to grasp early on.

For each Criteria/Or line, all the entries across that line are "ANDed"

"ORing" happens between between the lines

So the above is "saying"

{count = 5 and Deceased=False} or {Moved=False}

If Moved=False, it is going to ignore the count=5 filter!
Is that what you meant by "didn't do anything?"

My guess is that the following would work just as
well as the query with Count(*) in it.

Field: AnimalID AnimalDeceased AnimalMoved
Table: tblAnimals tblAnimals tblAnimals
Total: Count Group By Group By
Sort:
Show:
Criteria: 5 False
or: 5 False
or:

when you save the query, Access will always try to change

{count = 5 and Deceased=False}
or
{count = 5 and Moved=False}

to something it thinks is more efficient:

HAVING
(tblAnimals.AnimalDeceased=False
OR
tblAnimals.AnimalMoved=False)
AND
(Count(tblAnimals.AnimalID)=5);

and Access will be correct in this instance,
but you should always check your SQL View
if you have designed a complicated filter.
Access does not always know better than you
and can sometimes completely change the
logic of the filter.

So...(if I am thinking correctly)
it had nothing to do with Count(*);
it had to with understanding how
the grid works.

BTW, whenever you use "*" in any aggregate
function like Count, it will *not* ignore the Nulls.

My guess is that in your situation, there were
no Null AnimalID's in each group, so the two
"forms" were equivalent.

Count(*) would return the same values as
Count(tblAnimals.AnimalID)

Another thing to think about in group-by-query
grid design is that anything placed in Criteria/or
lines will show up in the HAVING clause.

This filtering happens *after* all the grouping
and aggregates have been processed. On a
large set of data, this can be expensive.

To filter before the grouping, you may need to
introduce a field again in another column of the
grid, and choose "Where" in the Totals row.

My guess is that you could have added two
new columns for Deceased and Moved, set
their Totals row to Where, Criteria row to False,
and removed False under their Group By columns
(so no need for second "5" under AnimalID).

FROM
tblOwners
INNER JOIN
tblAnimals
ON tblOwners.OwnerID = tblAnimals.OwnerID_AnimalTbl
WHERE
(tblAnimals.AnimalDeceased=False
OR
tblAnimals.AnimalMoved=False)
GROUP BY
tblAnimals.OwnerID_AnimalTbl,
tblOwners.OwnerID,
tblOwners.First,
tblOwners.Last,
tblAnimals.AnimalDeceased,
tblAnimals.AnimalMoved
HAVING
Count(tblAnimals.AnimalID)=5;

For other data, the different filters could
produce different counts, so you have to
think about it.

WHERE will set up whats in the groups...

HAVING will set up what groups to return...
 
G

Gary Walter

"Another thing to think about in group-by-query
grid design is that anything placed in Criteria/or
lines will show up in the HAVING clause."

should have been:

Another thing to think about in group-by-query
grid design is that anything placed in Criteria/or
lines will show up in the HAVING clause if the
Totals line is not "Where."
 
W

WCDoan

Thanks Gary for clearing that up. You're right. I knew about the OR and the
AND, but it never crossed my mind that without the 5 on both lines it wasn't
doing what I thought it was doing. Should've known better. I've been working
with Access about a year now, but not exclusively. In my job, I have to do
other things, so the learning curve kinda goes up and down. I get in the flow
of Access and then I have to do something totally different and by the time I
get back to having to work with Access some of what I had previously learned
has gone away and have to relearn it. But, it doesn't take as long as it did
initially. All you folks here in this discussion group have been great in
your willingness and patience in dealing with 'newbies' like myself and I am
so grateful to yall. I hope at some point I'll be able to join in the helping
process. Once again thanks so much for sharing your time and your knowledge.
RandyM
 
G

Gary Walter

One simple Count(*) test in NorthWinds db...

Open Orders table.

Go to bottom record, select
the entire record, and copy
to clipboard.

In "New" line, right-mouse click
and choose "Paste"

Find "Freight" in this new record
and delete it. So Freight is Null
for that record.

Then start a new query

SELECT
Orders.CustomerID,
Count(Orders.Freight) AS NoCntNulls,
Count(*) AS CntNulls
FROM Orders
GROUP BY
Orders.CustomerID
HAVING Count(Orders.Freight)<>Count(*)

Field: CustomerID Freight CntNulls: Count(*)
Table: Orders Orders
Total: Group By Count Expression
Sort:
Show:
Criteria: <>Count(*)
or:
 
G

Gary Walter

I get in the flow
of Access and then I have to do something totally different and by the
time I
get back to having to work with Access some of what I had previously
learned
has gone away and have to relearn it.
<snip>

I know the feeling.

After spending a few days writing stored
procs in SQL Server, I had trouble with
a simple If/Then/Else stmt in Access!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top