按日和小时平均SQL Server患者人口普查

时间:2016-11-23 18:45:57

标签: sql sql-server sql-server-2008

我需要创建一份患者人口普查报告,该报告显示在给定时间段内每小时和每周每天出现的平均患者人数。例如,这可以让我在过去的6个月中显示周一在ER中平均有4个人。我有一个表值函数,将为患者显示以下内容: VisitID,FromDateTime,ThruDateTime,LocationID。

我能够使用下面的代码显示某一天的患者数量,例如ER。但它仅限于一天。 (改编自http://www.sqlservercentral.com/Forums/Topic939818-338-1.aspx)。

--Census Count by Date Range--
DECLARE @BeginDateParameter DateTime, @EndDateParameter DateTime
SET @BeginDateParameter = '20160201'
SET @EndDateParameter = '2016-02-01 23:59:59.000'
----------------------------------------------------
-- Create a temp table to hold the necessary values
-- plus an extra "x" field to track processing
----------------------------------------------------
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL  DROP TABLE #Temp
CREATE TABLE #Temp (ID INT Identity NOT NULL, VisitID VarChar(100), SourceID VarChar(100),
  FromDateTime DateTime, ThruDateTime DateTime, x INT)

----------------------------------------------------
-- Populate the temp table with values from the
-- the actual table in the database
----------------------------------------------------
INSERT INTO #Temp
SELECT VisitID, FromDateTime, ThruDateTime
FROM PatientFlowTable(BeginDateParameter,@EndDateParameter)
WHERE (FromDateTime BETWEEN @BeginDateParameter AND @EndDateParameter +1
OR ThruDateTime BETWEEN @BeginDateParameter AND @EndDateParameter +1)
AND LocationID = 'ER'


-- Given Period is taken as inclusive of given hours in the input (eg. 15:25:30 will be taken as 15:00:00)
-- frist make sure that the minutes, seconds and milliseconds are removed from input range for clarity
set @BeginDateParameter = dateadd(hh, datepart(hh,@BeginDateParameter), convert(varchar(12),@BeginDateParameter,112))
set @EndDateParameter = dateadd(hh, datepart(hh,@EndDateParameter), convert(varchar(12),@EndDateParameter,112))

-- you may create this CTE by other ways (eg. from permanent Tally table)...
;with dh
as
(
select top 24 
    DATEADD(hour,ROW_NUMBER() OVER (ORDER BY [Object_id])-1,convert(varchar(12),@BeginDateParameter,112)) as HoDstart
   ,DATEADD(hour,ROW_NUMBER() OVER (ORDER BY [Object_id]),convert(varchar(12),@BeginDateParameter,112)) as HoDend
   ,ROW_NUMBER() OVER (ORDER BY Object_id)-1 as DayHour  
from sys.columns -- or any other (not very big) table which have more than 24 raws, just remamber to change 
                 -- [Object_id] in OVER (ORDER BY [Object_id]... to some existing column 
)
select d.DayHour, count(w.VisitID) as PatientCount
from dh d  
left join #Temp w
on    w.[FromDateTime] < d.HoDend 
  and w.[ThruDateTime] >= d.HoDstart  
where d.HoDstart between @BeginDateParameter and @EndDateParameter
group by d.DayHour
order by d.DayHour

SELECT VisitID, FromDateTime, ThruDateTime
FROM PatientFlowTable(BeginDateParameter,@EndDateParameter)
WHERE (FromDateTime BETWEEN @BeginDateParameter AND @EndDateParameter +1
OR ThruDateTime BETWEEN @BeginDateParameter AND @EndDateParameter +1)
AND LocationID = 'ER'

前三个小时的输出示例通过考虑他们的出发时间来显示急诊室中的患者。

Hour  PatientCount
0     2
1     3
2     3

1 个答案:

答案 0 :(得分:2)

为了查询短时间段,我会创建一个表值函数来生成小时条目。结果表可以加入您的查询。

CREATE FUNCTION [dbo].[f_hours] (@startDateTime DATETIME,
                                 @endDateTime   DATETIME)
RETURNS @result TABLE (
  [dateTime] DATETIME PRIMARY KEY
)
AS
BEGIN
  DECLARE
    @dateTime DATETIME = @startDateTime,
    @hours    INT      = DATEDIFF(hour, @startDateTime, @endDateTime)

  WHILE (@dateTime <= @endDateTime)
  BEGIN
    INSERT  
      INTO @result
      VALUES (@dateTime)

    SET @dateTime = DATEADD(hour, 1, @dateTime)
  END

  RETURN
END

GO

可以使用SET STATISTICS TIME ON输出功能所需的时间。为了生成超过6000条记录需要我的电脑53毫秒。

SET STATISTICS TIME ON

SELECT *
  FROM [dbo].[f_hours]('2016-02-01', '2016-02-10 16:00')

SET STATISTICS TIME OFF