The below query will pull back either user that are active in programs or those on their way to being active users in programs.
Query written in PostgreSQL
/*Pulls count of active user status in programs Only as current as the day your last export came to you...*/ Select pi.name as program_name,h.status,count(distinct p.validic_id) as user_count From patient_program_history as h /*To get latest upload time */ Inner Join patient_info as p on p.user_program_id = h.user_program_id /*To get program name */ Inner Join program_info as pi on pi.program_id = h.program_id /*To isolate last status update timestamp for users_program_id*/ Inner Join (Select user_program_id,max(updated_at) as updated_at From patient_program_history as h Group by user_program_id ) as max on (max.user_program_id = h.user_program_id AND max.updated_at = h.updated_at) /*To exclude user_program_id that isn't active or on the way to active*/ Where h.user_program_id not in (Select user_program_id From patient_program_history as h Where h.status in ('Unenrolled','Archived','Abandoned') Group by user_program_id) /*uncomment and add program name below if you want to isolate to one program*/ --and pi.name = '<Program_Name>' Group by pi.name ,h.status Order by 1,2;