There is a logical inconsistency in what you are currently asking the query
to do, in that you are restricting the results on two other columns, which
might mean that there are no rows with those values and the value of the
grouped column by which you are also restricting the query. Consequently no
row would be returned in which to include the zero count.
One way to do it would be to return the distinct value of the parameter in an
outer query, and the count of the rows which match the parameter value and
the other criteria in a subquery:
SELECT DISTINCT
[Forms]![frmUpdateCaseentry]![CaseID] AS ClmNum,
(SELECT COUNT(*)
FROM dbo_Diary
WHERE ClmNum = [Forms]![frmUpdateCaseentry]![CaseID]
AND Dtype = "Subro"
AND Dcomplete=0) AS DiaryCount
FROM dbo_Diary;
For convenience the outer query uses the same table as the subquery here, but
it could in fact use any table, so you might like to create a table, named
Dummy say, with one column and one row of an arbitrary value, and use that
table in the outer query, which would mean you could then dispense with the
DISTINCT option as the outer query can only return one row
If you did not want to restrict the query by the parameter, but return all
ClmNum values, you could of course correlate the subquery with the outer
query on the ClmNum column:
SELECT DISTINCT ClmNum,
(SELECT COUNT(*)
FROM dbo_Diary As D2
WHERE D2.ClmNum = D1.ClmNum
AND Dtype = "Subro"
AND Dcomplete=0) AS DiaryCount
FROM dbo_Diary AS D1;
In this case the outer query and subquery must use the same table of course,
differentiating them with aliases D1 and D2.
Ken Sheridan
Stafford, England
I need to know when a specfic count is zero. However, when I use the 'count'
function, if its zero, I simply don't get anything. I have no sceond table,
so an outer join won't do the trick for me. my SQL is below. Thanks for
your help!
SELECT Count(dbo_Diary.DiaryID) AS CountOfDiaryID, dbo_Diary.ClmNum
FROM dbo_Diary
WHERE (((dbo_Diary.Dtype)="Subro") AND ((dbo_Diary.Dcomplete)=0))
GROUP BY dbo_Diary.ClmNum
HAVING (((dbo_Diary.ClmNum)=[Forms]![frmUpdateCaseentry]![CaseID]));