A query query (1 Viewer)

John Sh

Member
Local time
Tomorrow, 07:51
Joined
Feb 8, 2021
Messages
410
I have a "Yes/No" field in a table and I want to count the "Yes's" with a query.
I have three records as "yes"

If I use "Count" and a criteria of "False or No" it returns null.
If I use a criteria of "True or Yes" it returns the total records in the table.

If I use "Sum" and a criteria of "True or Yes" it returns -3, negative 3.
If I use a criteria of "True or Yes" it returns null.

I could use "sum" and multiply by -1 but that seems a bit ridiculous.
Where am I going wrong?
I know I can use dCount to get the right answer but doing so creates other problems with the interchangeability of the code.

Code:
SELECT Count(Main.Label) AS CountOfLabel
FROM Main
HAVING (((Count(Main.Label))=Yes));


SELECT Sum(Main.Label) AS SumOfLabel
FROM Main
HAVING (((Sum(Main.Label))=Yes));
 

June7

AWF VIP
Local time
Today, 13:51
Joined
Mar 9, 2014
Messages
5,474
SELECT Count(*) AS TotYes FROM Main WHERE Label=True;

SELECT Sum(Abs(Label)) AS TotYes FROM Main;

SELECT Count(*) AS CntAll, Sum(Abs(Label)) AS CntYes, Sum(IIf(Not Label, 1, 0)) AS CntNo FROM Main;
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 22:51
Joined
Jan 14, 2017
Messages
18,235
Code:
SELECT Count(FieldName) AS CountOfFieldName
FROM TableName
WHERE FieldName=True;

Code:
SELECT Count(FieldName) AS CountOfFieldName
FROM TableName
WHERE FieldName=False;
 

John Sh

Member
Local time
Tomorrow, 07:51
Joined
Feb 8, 2021
Messages
410
Thank you both.
The obvious is often the hardest to see!
 

Users who are viewing this thread

Top Bottom