【发布时间】:2015-09-11 21:05:08
【问题描述】:
我有一个查询,要从我们的医疗记录系统中提取特定时间段内提供者看到的患者人数。我想每个月都提取过去一年的数据,包括本月至今。
我可以手动进行查询,但我希望它每月自动调整,以便当前月份是最后一个查询,其他月份是前 11 个月,而无需重做代码中的所有搜索.
理想情况下,我还希望将其输出到我的表格中,并带有相应的月份标题。例如,当表格输出时,本月将列为 9/15 而不是 Month12,这将调整为下个月的 10/15,依此类推。
关于如何完成这两项任务的任何建议?
这是我当前的代码,它运行良好,但必须每个月手动调整:
BEGIN
INSERT INTO #Output (Provider, Date1, Date2, Date3, Date4, Date5, Date6, Date7, Date8, Date9, Date10, Date11, Date12)
OUTPUT inserted.Provider, inserted.Date1, inserted.Date2, inserted.Date3, inserted.Date4, inserted.Date5, inserted.Date6, inserted.Date7, inserted.Date8,
inserted.Date9, inserted.Date10,inserted.Date11, inserted.Date12
select pm.last_name,
SUM(case when enc_timestamp >= '20141001' and enc_timestamp < '20141101' then 1 else 0 end) as total_patients_Oct14,
SUM(case when enc_timestamp >= '20141101' and enc_timestamp < '20141201' then 1 else 0 end) as total_patients_Nov14,
SUM(case when enc_timestamp >= '20141201' and enc_timestamp < '20150101' then 1 else 0 end) as total_patients_Dec14,
SUM(case when enc_timestamp >= '20150101' and enc_timestamp < '20150201' then 1 else 0 end) as total_patients_Jan15,
SUM(case when enc_timestamp >= '20150201' and enc_timestamp < '20150301' then 1 else 0 end) as total_patients_Feb15,
SUM(case when enc_timestamp >= '20150301' and enc_timestamp < '20150401' then 1 else 0 end) as total_patients_Mar15,
SUM(case when enc_timestamp >= '20150401' and enc_timestamp < '20150501' then 1 else 0 end) as total_patients_April15,
SUM(case when enc_timestamp >= '20150501' and enc_timestamp < '20150601' then 1 else 0 end) as total_patients_May15,
SUM(case when enc_timestamp >= '20150601' and enc_timestamp < '20150701' then 1 else 0 end) as total_patients_June15,
SUM(case when enc_timestamp >= '20150701' and enc_timestamp < '20150801' then 1 else 0 end) as total_patients_July15,
SUM(case when enc_timestamp >= '20150801' and enc_timestamp < '20150901' then 1 else 0 end) as total_patients_Aug15,
SUM(case when enc_timestamp >= '20150901' and enc_timestamp < '20151001' then 1 else 0 end) as Total_Charge_Sep15
from patient_encounter pe
inner join provider_mstr pm ON pe.rendering_provider_id = pm.provider_id
where enc_timestamp >= '20140101' and enc_timestamp < '20160101' and billable_ind = 'Y' and checkin_datetime is not NULL
group by pm.last_name
END
【问题讨论】:
标签: sql-server date datetime