ms access - COUNT evaluate to zero if no matching records -


take following:

select      count(a.record_id) newrecruits     ,a.studyrecord_id      visits  inner join  (     select          record_id         , max(modtime) latest               visits      group          record_id )  b  on (a.record_id = b.record_id) , (a.modtime = b.latest) (((a.visit_type_id)=1)) group a.studyrecord_id; 

i want amend count part display 0 if there no records since assume count evaluate null.

i have tried following still no results:

iif(isnull(count(a.record_id)),0,count(a.record_id)) newrecruits 

is issue because join on record_id? tried changing inner left received no results.

q how above evaluate 0 if there no records matching criteria?

edit:

to give little detail reasoning.

the studies table contains field called 'original_recruits' based on activity before use of database.

the visits tables tracks new_recruits (count of records each study).

i combine these in query (original_recruits + new_recruits)- if there have been no new recruits still need display original_recruits if there no records need evalulate 0 instead of null final sum still works.

it seems want count records studyrecords.
if need count of 0 when have no records, need join table named studyrecords.
did have one? else nonsense ask rows when don't have rows!

let's suppose studyrecords exists, query should :

select      count(a.record_id) newrecruits -- a.record_id null if there 0 count studyrecord, else contain id     sr.id      visits      inner join      (         select              record_id             , max(modtime) latest                       visits          group              record_id     )  b      on (a.record_id = b.record_id) , (a.modtime = b.latest)     left outer join studyrecord sr         on sr.id = a.studyrecord_id a.visit_type_id = 1 group sr.id 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -