按小时计算急诊室的患者数量



我想在给定的时间跨度(周、月或年(内按小时计算急诊室中目前患者的数量或平均值(如果可能的话(,并且我无法概念化有效的代码(不仅仅是注册数量(。

我有两个时间变量,"签到时间"和"发布时间"。这些日期/时间变量显然是任意的,"发布时间"变量将出现在"签入时间变量"之后。

示例数据

 Patient_ID    'Check In Time'                 'Release Time'
 01           2015-08-01 02:49:00         2015-08-01 08:29:00
 02           2015-08-02 01:30:00         2015-08-02 14:29:00
 03           2015-08-02 21:30:00         2015-09-02 01:20:00

我希望给定日期的输出如下所示:

Hour        Midnight   1am   2am   3am    4am.....
# of Pts      34       56     89    23     29

例如,在凌晨1点,考虑到登记和释放时间,目前有56名患者在急诊室。

我最初的想法是:1(对两个变量进行四舍五入2(编写代码,看起来像这样...

  select Pt_fin
  case when  checkin like '1am' and release like '2am' then '1' else '0' 
  end OneAMToTwoAM,
  case when  checkin like '1am' and release like '2am' then '1' else '0' 
  end OneAMToTwoAM,
  case when  checkin like '1am' and release like '2am' then '1' else '0' 
  end TowAMToThreeAM
  from ED

....

然而,这让我停顿了一下,因为我觉得有一种更有效的方法!

提前感谢!

这可以解决您的查询,但列名必须没有空格,请尝试更改它!

select 
Patient_ID
,count(case when DATEPART(hh, 'Check In Time') >= 0 and DATEPART(hh,'Release Time')         <= 1 then 1 else 0 end) Midnight   
,count(case when DATEPART(hh, 'Check In Time') >= 1 and DATEPART(hh,'Release Time')     <= 2 then 1 else 0 end) 1AM   
,count(case when DATEPART(hh, 'Check In Time') >= 2 and DATEPART(hh,'Release Time')     <= 3 then 1 else 0 end) 2AM   
,count(case when DATEPART(hh, 'Check In Time') >= 3 and DATEPART(hh,'Release Time')     <= 4 then 1 else 0 end) 3AM   
from ED
group by Patient_ID

我希望这有帮助!

最新更新