我希望看到今年与去年相比,给定日期范围内每月就诊的初诊患者数量,并将其与相同日期范围内每月就诊的患者总数进行比较。
我可以设置如下的第一次检查患者:
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列:月、今年、今年、去年、去年
但似乎不知道怎么做。列的顺序并不重要。
可能是:月,今年,去年,今年,去年
最佳答案
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
关于sql - 显示PostgreSQL中多个列的每月总计,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25139715/