2016-11-23 39 views
0

我需要创建患者人口普查报告,显示给定时间段内每小时和每周每天的平均患者人数。例如,这可以让我显示,在过去的6个月中,星期一的急诊室平均有4人。我有一个表值函数,将显示以下患者: VisitID,FromDateTime,ThruDateTime,LocationID。SQL Server患者普查平均按天和小时

我能够使用下面的代码显示例如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' 

前3个小时的输出示例显示出现在ER中的患者考虑到他们的出发时间。

Hour PatientCount 
0  2 
1  3 
2  3 
+0

这是不是很清楚你在问什么。 – mendosi

回答

1

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

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 ms。

SET STATISTICS TIME ON 

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

SET STATISTICS TIME OFF 
+0

非常感谢!一旦我将您的表值函数加入#Temp,我就能够显示每周和每小时患者的平均计数。完善! – MCW3u