在SQL中,我试图找到目前在我们急诊科的患者人数。这是我到目前为止的查询:
SELECT CAST(dt_ARRIVAL AS DATE) [Date],
DATEPART(HOUR,dt_ARRIVAL) [Hour], Count(1) [Patients]
FROM [Hospital].[dbo].[DATA_DUMP]
WHERE dt_ARRIVAL >= '2016-04-15'
GROUP BY CAST(dt_ARRIVAL AS DATE), DATEPART(HOUR,dt_ARRIVAL)
ORDER BY 1 ,2
它给出了我在那个时间到达的患者数量,如下所示:
Date Hour Patients
4/15/16 2 2
4/15/16 4 1
4/15/16 6 1
4/15/16 7 5
4/15/16 8 2
这并不能解释所有患者,例如床上或候诊室内的患者。我以为我可以使用"离开"时间戳叫做dt_departure来计算这个,但这就是我被困住的地方。非常感谢任何帮助。
以下是我擦除患者标识符的表格中的一些数据:
dt_ARRIVAL dt_DEPARTURE s_OUTCOME_LOCATION _OUTCOME_ID
4/20/2016 4:49 4/20/2016 5:24 D:Home/Self Care 2
4/20/2016 5:49 4/20/2016 9:30 H:Med-Surg 2, CPH213-1 1
4/20/2016 7:01 4/20/2016 11:10 H:Observation, OBS250-3 1
4/20/2016 7:34 NULL NULL NULL
答案 0 :(得分:0)
如果没有看到你的数据结构,那就有点难了,但是
SELECT CAST(dt_ARRIVAL AS DATE) [Date],
DATEPART(HOUR,dt_ARRIVAL) [Hour], Count(1) [Patients]
FROM [Hospital].[dbo].[DATA_DUMP]
WHERE dt_ARRIVAL >= '2016-04-15' AND dt_DEPART=''
GROUP BY CAST(dt_ARRIVAL AS DATE), DATEPART(HOUR,dt_ARRIVAL)
ORDER BY 1 ,2
我的假设是dt_DEPART=''
之类的东西会排除已经退出的病人。
答案 1 :(得分:0)
SELECT CAST(dt_ARRIVAL AS DATE) [Date]
, DATEPART(HOUR,dt_ARRIVAL) [Hour], Count(1) [Patients]
FROM [Hospital].[dbo].[DATA_DUMP]
WHERE dt_ARRIVAL >= '2016-04-15'
GROUP BY CAST(dt_ARRIVAL AS DATE), DATEPART(HOUR,dt_ARRIVAL)
union all
select cast('1900-01-01' as date), 0, count(*)
FROM [Hospital].[dbo].[DATA_DUMP]
WHERE dt_ARRIVAL is null
ORDER BY 1 ,2;
但是我很惊讶你可以让某人在没有dt_ARRIVAL的情况下进入 怎么会有人坐在没有dt_ARRIVAL的床上呢? 似乎应该是必填字段