显示PostgreSQL中多列的每月总计

时间:2014-08-05 13:11:52

标签: sql postgresql date aggregate-functions

我希望看到今年特定日期范围内每月看到的第一例患者数量与去年相比,并将其与同一日期范围内每月所见患者总数进行比较。

我能够按如下方式建立第一例患者:

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

这给了我三列:月,今年,去年。

请注意:我在月份名称之前输入数字,因为我无法按时间顺序显示月份。我们将不胜感激任何在月之前未显示数字的提示。

我想为整个患者增加两列 - 例如:

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

结果分为5列:月,今年,总年,历年,总年...

但似乎无法确切知道如何做到这一点。列的顺序并不重要。 它可能是:月,今年,去年,总共今年,总年...

1 个答案:

答案 0 :(得分:2)

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