A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

counts NULL values in query



 
 
Thread Tools Display Modes
  #1  
Old April 29th, 2010, 02:56 PM posted to microsoft.public.access.queries
Nathan
external usenet poster
 
Posts: 177
Default counts NULL values in query

I am suing Access 2003.

I need to a list of all "cases", and a count of "diaries" for each case, and
I need a zero for any null values (ie, zero diaries for that case).

I'm trying the 'Nz' function in sql, but its not working. Any ideas?

SELECT dbo_Case.CaseID, Nz(Count(dbo_Diary.DiaryID),0) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((dbo_Diary.Dcomplete)=0) AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

  #2  
Old April 29th, 2010, 03:05 PM posted to microsoft.public.access.queries
Nathan
external usenet poster
 
Posts: 177
Default counts NULL values in query

HAHA - I just reread my post - the first line should say, "I am USING Access
2003". Freudian slip Althought, I'd like to sue them sometimes...


"Nathan" wrote:

I am suing Access 2003.

I need to a list of all "cases", and a count of "diaries" for each case, and
I need a zero for any null values (ie, zero diaries for that case).

I'm trying the 'Nz' function in sql, but its not working. Any ideas?

SELECT dbo_Case.CaseID, Nz(Count(dbo_Diary.DiaryID),0) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((dbo_Diary.Dcomplete)=0) AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

  #3  
Old April 29th, 2010, 04:06 PM posted to microsoft.public.access.queries
Daryl S[_2_]
external usenet poster
 
Posts: 881
Default counts NULL values in query

Nathan -

Count will take into account the null records. I think your problem was the
criteria that dbo_Diary.Dcomplete = 0. That would exclude null records.
Change that to use the nz, like this (untested):

SELECT dbo_Case.CaseID, Count(dbo_Diary.DiaryID) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((nz(dbo_Diary.Dcomplete,0))=0)
AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

--
Daryl S


"Nathan" wrote:

I am suing Access 2003.

I need to a list of all "cases", and a count of "diaries" for each case, and
I need a zero for any null values (ie, zero diaries for that case).

I'm trying the 'Nz' function in sql, but its not working. Any ideas?

SELECT dbo_Case.CaseID, Nz(Count(dbo_Diary.DiaryID),0) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((dbo_Diary.Dcomplete)=0) AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

  #4  
Old April 29th, 2010, 08:00 PM posted to microsoft.public.access.queries
Nathan
external usenet poster
 
Posts: 177
Default counts NULL values in query

Thanks Daryl, this seemed to work. I don't understand WHY it worked, but it
worked nonetheless.

"Daryl S" wrote:

Nathan -

Count will take into account the null records. I think your problem was the
criteria that dbo_Diary.Dcomplete = 0. That would exclude null records.
Change that to use the nz, like this (untested):

SELECT dbo_Case.CaseID, Count(dbo_Diary.DiaryID) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((nz(dbo_Diary.Dcomplete,0))=0)
AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

--
Daryl S


"Nathan" wrote:

I am suing Access 2003.

I need to a list of all "cases", and a count of "diaries" for each case, and
I need a zero for any null values (ie, zero diaries for that case).

I'm trying the 'Nz' function in sql, but its not working. Any ideas?

SELECT dbo_Case.CaseID, Nz(Count(dbo_Diary.DiaryID),0) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((dbo_Diary.Dcomplete)=0) AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

  #5  
Old April 29th, 2010, 08:25 PM posted to microsoft.public.access.queries
Daryl S[_2_]
external usenet poster
 
Posts: 881
Default counts NULL values in query

Nathan -

Using an outer join (LEFT or RIGHT) normally would mean you take all the
records from the (LEFT or RIGHT) table and only those from the other table
that match. If there aren't records from the 'other' table, then those
fields are left blank, but the records from the LEFT or RIGHT table are
included.

The original problem in your code was that you required
dbo_Diary.Dcomplete=0, and this is not a criteria of the LEFT or RIGHT table,
but a criteria on the 'other' table. This means if there is a null value for
Dcomplete (due to a null in the table or in this case no matching record),
then this record will be excluded from the query results.

By adding the nz(Dcomplete,0) to the field, we are saying if this field is
null (either null value or no matching record), then treat it as if it were a
zero. Then your criteria of only including records where Dcomplete = 0 would
be true for these values, and the records would be included in the query
results.

Hope that helps!

--
Daryl S


"Nathan" wrote:

Thanks Daryl, this seemed to work. I don't understand WHY it worked, but it
worked nonetheless.

"Daryl S" wrote:

Nathan -

Count will take into account the null records. I think your problem was the
criteria that dbo_Diary.Dcomplete = 0. That would exclude null records.
Change that to use the nz, like this (untested):

SELECT dbo_Case.CaseID, Count(dbo_Diary.DiaryID) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((nz(dbo_Diary.Dcomplete,0))=0)
AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

--
Daryl S


"Nathan" wrote:

I am suing Access 2003.

I need to a list of all "cases", and a count of "diaries" for each case, and
I need a zero for any null values (ie, zero diaries for that case).

I'm trying the 'Nz' function in sql, but its not working. Any ideas?

SELECT dbo_Case.CaseID, Nz(Count(dbo_Diary.DiaryID),0) AS CountOfDiaryID
FROM dbo_Diary RIGHT JOIN dbo_Case ON dbo_Diary.ClmNum = dbo_Case.CaseID
WHERE (((dbo_Case.CaseEEID)="gclcalg") AND ((dbo_Diary.Dcomplete)=0) AND
((dbo_Case.CaseStatus)6 And (dbo_Case.CaseStatus)9))
GROUP BY dbo_Case.CaseID;

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:16 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.