kipsoft
kipsoft

Reputation: 359

Showing Monthly Totals from Multiple Columns in PostgreSQL

I am looking to see the number of firstexam patients seen per month for a given date range this year vs last year, and compare that to the total number of patients seen per month for the same date range.

I am able to set up the firstexam patients as follows:

select case EXTRACT(month FROM patient_info.firstexam)
    when 1 then '01 - January'
    when 2 then '02 - February'
    when 3 then '03 - March'
    when 4 then '04 - April'
    when 5 then '05 - May'
    when 6 then '06 - June'
    when 7 then '07 - July'
    when 8 then '08 - August'
    when 9 then '09 - September' 
    when 10 then '10 - October'
    when 11 then '11 - November'
    when 12 then '12 - December'
end as month,
sum(case when patient_info.firstexam >= '2013-01-01' AND patient_info.firstexam <= '2013-12-31' then 1 else 0 end) thisyear,
sum(case when patient_info.firstexam >= '2012-01-01' AND patient_info.firstexam <= '2012-12-31' then 1 else 0 end) lastyear

from patient_info WHERE (patient_info.firstexam >= '2013-01-01' AND patient_info.firstexam <= '2013-12-31' OR patient_info.firstexam >= '2012-01-01' AND patient_info.firstexam <= '2012-12-31') 

GROUP BY month
ORDER BY month

This gives me three columns: month, thisyear, lastyear.

Please note: I entered numerical month before month name because I could not get the months to appear in chronological order otherwise. Any hints to not show the number before the month would be appreciated.

I would like to add two more columns for total patients - something like:

select case EXTRACT(month FROM patient_info.lastexam)
    when 1 then '01 - January'
    when 2 then '02 - February'
    when 3 then '03 - March'
    when 4 then '04 - April'
    when 5 then '05 - May'
    when 6 then '06 - June'
    when 7 then '07 - July'
    when 8 then '08 - August'
    when 9 then '09 - September' 
    when 10 then '10 - October'
    when 11 then '11 - November'
    when 12 then '12 - December'
end as month,
sum(case when patient_info.lastexam >= '2013-01-01' AND patient_info.lastexam <= '2013-12-31' then 1 else 0 end) totalthisyear,
sum(case when patient_info.lastexam >= '2012-01-01' AND patient_info.lastexam <= '2012-12-31' then 1 else 0 end) totallastyear

from patient_info WHERE (patient_info.lastexam >= '2013-01-01' AND patient_info.lastexam <= '2013-12-31' OR patient_info.lastexam >= '2012-01-01' AND patient_info.lastexam <= '2012-12-31') 

GROUP BY month
ORDER BY month

with the results in 5 columns: month, thisyear, totalthisyear, lastyear, totallastyear

but can't seem to figure out exactly how this could be done. The order of the columns is not important. It could be: month, thisyear, lastyear, totalthisyear, totallastyear

Upvotes: 1

Views: 149

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125234

SQL Fiddle

select
    to_char(('2012-' || m || '-01')::date, 'Month'),
    thisyear, lastyear, totalthisyear, totallastyear
from (
    select
        extract(month from m) as m,
        sum(case
            when firstexam between '2013-01-01' and '2013-12-31' then firstexam_count
            else 0 end
        ) as thisyear,
        sum(case
            when firstexam between '2012-01-01' and '2012-12-31' then firstexam_count
            else 0 end
        ) as lastyear,
        sum(case
            when lastexam between '2013-01-01' and '2013-12-31' then lastexam_count
            else 0 end
        ) as totalthisyear,
        sum(case
            when lastexam between '2012-01-01' and '2012-12-31' then lastexam_count
            else 0 end
        ) as totallastyear
    from
        generate_series (
            '2012-01-01'::date, '2013-12-31', '1 month'
        ) g(m)
        left join (
            select count(*) as firstexam_count, date_trunc('month', firstexam) as firstexam
            from patient_info
            where firstexam between '2012-01-01' and '2013-12-31'
            group by 2
        ) pif on firstexam = m
        left join (
            select count(*) as lastexam_count, date_trunc('month', lastexam) as lastexam
            from patient_info
            where lastexam between '2012-01-01' and '2013-12-31'
            group by 2
        ) pil on lastexam = m
    group by 1
) s
order by m

Upvotes: 2

Related Questions