PC Review


Reply
Thread Tools Rate Thread

counts NULL values in query

 
 
Nathan
Guest
Posts: n/a
 
      29th Apr 2010
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;

 
Reply With Quote
 
 
 
 
Nathan
Guest
Posts: n/a
 
      29th Apr 2010
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;
>

 
Reply With Quote
 
Daryl S
Guest
Posts: n/a
 
      29th Apr 2010
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;
>

 
Reply With Quote
 
Nathan
Guest
Posts: n/a
 
      29th Apr 2010
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;
> >

 
Reply With Quote
 
Daryl S
Guest
Posts: n/a
 
      29th Apr 2010
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;
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null or No Null values in a parameter query tim Microsoft Access Getting Started 6 13th Aug 2008 10:43 PM
Q: crosstab query with expression for values, not just counts =?Utf-8?B?TWFyaw==?= Microsoft Access Queries 4 14th Dec 2005 04:12 AM
Counting Null and Not Null values in one query Amy Johnson Microsoft Access Queries 6 20th Nov 2004 05:55 AM
How Do I Display Null Values for counts =?Utf-8?B?U29uZ29rdQ==?= Microsoft Access Queries 1 20th Oct 2004 02:59 PM
In Crosstab Query If Any Values Are Null I Want Sum to Be Null mcl Microsoft Access Queries 4 27th Feb 2004 06:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 PM.