【问题标题】:Showing Monthly Totals from Multiple Columns in PostgreSQL在 PostgreSQL 中显示多列的月度总计
【发布时间】:2014-08-05 13:11:51
【问题描述】:

我希望查看今年与去年在给定日期范围内每月就诊的 firstexam 患者数量,并将其与同一日期范围内每月就诊的患者总数进行比较。

我可以如下设置 firstexam 患者:

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 列:月份、今年、总计今年、去年、总计去年

但似乎无法确切地弄清楚如何做到这一点。列的顺序并不重要。 可能是:月、今年、去年、今年总计、去年总计

【问题讨论】:

    标签: sql postgresql date aggregate-functions


    【解决方案1】:

    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
    

    【讨论】:

    • 不知道为什么,但第一列(今年)是唯一给我正确计数的列。所有其他列的实际数字都低于上面返回的代码。例如,上面的代码返回 148、230、360、238 年 1 月,但是当我单独搜索时,实际数字应该是 148、179、275、203。有什么想法可能是错误的来源吗?
    • @kipsoft 当我单独搜索时查询是什么?
    • SELECT firstexam from patient_info where firstexam between '2013-01-01' and '2013-01-31' (我使用返回的记录数进行计数),然后我只是更改日期范围从 2013 年到 2012 年以及从 firstexam 到 lastexam 的字段以获得其他结果。
    • @kipsoft 好的,检查新版本。
    • 我有一个一直在努力解决的问题。我没有从 Patient_info 表中计算 lastexam,而是尝试从 transactions 表中计算唯一的患者就诊次数。 Firstexam 数字很好,但并非所有用户都会在每笔交易中更新 lastexam 列 - 所以我需要修改它,以便每个月的患者总数是当月患者就诊的总数。这通常会被计为交易总数,但是,有些患者可能会在一天内收到两张发票,而这只会被计为一次患者就诊)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多