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

时间:2019-04-30 15:49:31

标签: sql-server

我想在给定的时间范围(周,月或年)中按小时计算当前急诊室中当前患者的数量或平均值(如果可能的话),而在概念化高效代码方面遇到困难(不仅是注册数量)。

我有两个时间变量,“签到时间”和“发布时间”。这些日期/时间变量显然是任意的,并且“发布时间”变量将位于“签入时间变量”之后。

示例数据

 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

....

但是,这让我停下来,因为我觉得有一种更有效的方法!

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

我希望这会有所帮助!