3

I'm trying to group patient readmissions that occur within separate 30 day windows (SQL Server 2008). After multiple attempts at recursive CTE's, I'm just going in circles. All help is greatly appreciated.

Problem:

If a patient is admitted within 30 days of the first discharge, count all those admissions in the first period (in the example below, the second admit date of 7/7/2011 is within the 7/20/2011 window).

The first admit immediately after the above 30 day period should be treated as a new 30 day window and any admits within 30 days of its discharge become part of group 2, etc.

So, record 3 is the start of a new 30 day window, even though this admit is within 30 days of the discharge of the prior record (admit on 8/5/2011 is before 8/26/2011, but 8/5/2011 is greater than the 7/20/2011 terminus that started the first 30 day window).

Record 4 was admitted prior to 8/31/2011, therefore it should be included in group 2.

Record 5 stands alone because it was admitted more than 30 days after the 8/31/2011 date that ended group 2.

The desired output for the example recordset is the sum of charges for each 30 day start point.

Desired result:

MRN   Admit      TotalCharge
555   6/14/2011  $25
555   7/30/2011  $39
555   11/3/2011  $10

Example recordset:

Acct MRN    Admit       Disc        Disch+30    Charge
590  555    6/14/2011   6/20/2011   7/20/2011   15
938  555    7/7/2011    7/27/2011   8/26/2011   10
1011 555    7/30/2011   8/1/2011    8/31/2011    9
1089 555    8/5/2011    9/14/2011   10/14/2011  30
3011 555    11/3/2011   11/23/2011  12/23/2011  10
4

2 回答 2

2

看起来我可能有点野心勃勃,坚持认为这可以通过单通道、基于集合的查询来完成。这非常接近,但仍将结果转储到 #temp 表以支持准并行更新(每个 MRN 值一个“线程”,而不是一次通过一个 MRN 的游标)。

DECLARE @t TABLE(Acct INT, MRN INT, Admit DATE, Disc DATE, Charge INT);

INSERT @t VALUES
(590 , 555, '20110614','20110620',15), 
(938 , 555, '20110707','20110727',10),
(1011, 555, '20110730','20110801', 9),
(1089, 555, '20110805','20110914',30),
(3011, 555, '20111103','20111123',10);

SELECT MRN, [group] = CONVERT(INT, NULL), Admit, Disc, Charge, 
   rn = ROW_NUMBER() OVER (PARTITION BY MRN ORDER BY Admit),
  da = DATEADD(DAY, 30, Disc)
  INTO #x FROM @t; -- add a WHERE clause if examining a set in a bigger table

DECLARE @rn INT = 0;

WHILE @rn IS NOT NULL
BEGIN
  SELECT @rn = MIN(rn) FROM #x WHERE [group] IS NULL;

  UPDATE agg SET [group] = @rn
    FROM #x AS agg
    INNER JOIN #x AS src
    ON agg.MRN = src.MRN
    AND agg.Admit <= src.da
    AND agg.[group] IS NULL
    AND src.rn = @rn;
END
GO
SELECT MRN, Admit = MIN(Admit), TotalCharge = SUM(Charge)
  FROM #x GROUP BY MRN, [group];
GO
DROP TABLE #x;

结果:

MRN  Admit       TotalCharge
---  ----------  -----------
555  2011-06-14  25
555  2011-07-30  39
555  2011-11-03  10
于 2013-04-15T22:13:01.260 回答
0

对自身执行日期表的自联接,以识别所有在 30 天内没有被另一次录取的录取;调用该关系 T1 并使其成为主查询的子查询。

现在再次将 T1 加入数据,以从 T1 中每一行的数据中获取所有二次录取。

今晚晚些时候我可能有时间再看一遍。

于 2013-04-15T22:06:36.990 回答