显示PostgreSQL中多个列的月度总计
我希望看到在给定日期范围内,今年与去年每月就诊的首次检查患者数量,并将其与相同日期范围内每月就诊的患者总数进行比较 我可以按如下方式设置首次检查患者:显示PostgreSQL中多个列的月度总计,sql,postgresql,date,aggregate-functions,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
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
选择病例摘录(患者信息第一次检查的月份)
当1然后是'01-一月'
当2时,然后是'02-二月'
3月3日,然后是'03-3月'
当4时,然后是'04-April'
5月5日,然后是5月5日
6月6日,然后是'06-6月'
7时,然后是'07-7'
8时,然后是'08-8月'
9时,然后是'09-9'
10时,然后是“10月10日”
11月11日,然后是“11月11日”
12月12日,然后是“12月12日”
月底,
总和(当患者信息第一次检查>='2013-01-01'和患者信息第一次检查='2012-01-01'和患者信息第一次检查='2013-01-01'和患者信息第一次检查='2012-01-01'和患者信息第一次检查='2013-01-01'和患者信息第一次检查='2012-01-01'和患者信息第一次检查='2013-01-01'和患者信息第一次检查='2012-01-01'和患者信息第一次检查='2012-01-01'ent_info.lastexam
不知道为什么,但是第一栏(今年)是唯一给出正确计数的列。所有其他列的实际数字都低于上面返回的代码。例如,上面的代码返回的是1月148、230、360、238,但当我单独搜索时,实际数字应该是148、179、275、203。知道错误可能来自何处吗?@kipsoft什么时候我单独搜索查询?从患者信息中选择firstexam,其中firstexam介于“2013-01-01”和“2013-01-31”之间(我使用为计数返回的记录数),然后我只是将日期范围从2013年更改为2012年,并将字段从firstexam更改为lastexam以获得其他结果。@kipsoft Ok检查新版本。我有一个问题一直困扰着我。我不是从patient_info表中计算lastexam,而是尝试从transactions表中计算唯一的患者就诊次数。Firstexam数字可以,但并非所有用户都会在每笔交易中更新LastCheck列-因此我需要修改此值,以便每个月的总患者数是该月的总患者就诊数。这通常会算作交易总数,但是,有些患者可能会在一天内收到两张发票,而这只会是共同的未测试为一次患者就诊)。
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
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