SQL: Patient Age Distribution by PCP

About

Shows active patients bucketed by age groups and by PCP.  

A sample image of this SQL report run in the Database Viewer is shown below:


Caveats

None

Code

To highlight and copy the code below to your clipboard, simply click the Copy button.

with pts as (
select a.* , staffname, case when age<1 then '<1' when age<6 then '1-5' when age<11 then '6-10' when age <15 then '11-14' when age <22 then '15-21' else '22+'  end as bracket  from (
select patno, addr_id, floor(datediff(month, dateadd(day,-extract(day from birthdat)+1,birthdat),  dateadd(day,-extract (day from birthdat)+1,cast('today' as date)) )/12) as age from register
where patno >99 and status_pat = 'ACTIVE') a
inner join staff1 on staff1.staffid = a.addr_id
)
select count(patno), bracket, staffname from pts group by bracket, staffname