2016-08-08 5 views
1

Ich suche die Tage zwischen Ergebnis Daten für jeden Patient zu bekommen: nur bei Ergebnis Daten, wo der Ergebniswert < 90,00Wie kann ich die Tage zwischen den Ergebnisdaten für jede einzelne pat_id erhalten?

;WITH patient_results AS (
SELECT * FROM (VALUES 
(1, 'EA11AEE3-1D90-4602-9A37-0000007E2293', '85.10' ,'2015-12-11'), 
(1, '27BCD3E4-2381-4139-B420-0000025B4113', '91.50' ,'2016-01-05'), 
(1, 'D8969360-45D6-487B-AF94-0000035F78B0', '81.00' ,'2016-07-21'), 
(5, '446E6413-442A-452A-BCF4-000006AA9896', '58.00' ,'2014-07-01'), 
(5, '00305129-BC14-4A12-8368-00000AC04A9B', '53.00' ,'2014-12-13'), 
(5, '96A67E53-2D6C-430B-A01F-00000AE4C37B', '42.80' ,'2015-02-01'), 
(5, '7C330511-3E99-488C-AF5E-00000BDFA3FF', '54.00' ,'2015-07-01'), 
(8, '62A2806A-4969-417A-B4DF-D547621CC594', '89.00' ,'2016-03-10'), 
(8, '3B9F4E5B-3433-4F21-850A-FC2127A24B72', '92.60' ,'2016-06-30'), 
(8, '1A2D780D-8C11-451C-8A64-6D49140B6232', '88.00' ,'2016-08-05') 
) as t (pat_id, visit_id, result_value, result_date)) 

Basierend auf der oben ist suchen, so etwas zu bekommen:

PAT_ID | VISIT_ID        | RESULT_VALUE | RESULT_DATE| DAYSBETWEENRESULTDATES 
1  | EA11AEE3-1D90-4602-9A37-0000007E2293 | 85.10   | 2015-12-11  | 0 
1  | D8969360-45D6-487B-AF94-0000035F78B0 | 81.00   | 2016-07-21  | 223 
5  | 446E6413-442A-452A-BCF4-000006AA9896 | 58.00   | 2014-07-01  | 0 
5  | 00305129-BC14-4A12-8368-00000AC04A9B | 53.00   | 2014-12-13  | 165 
5  | 96A67E53-2D6C-430B-A01F-00000AE4C37B | 42.80   | 2015-02-01  | 50 
5  | 7C330511-3E99-488C-AF5E-00000BDFA3FF | 54.00   | 2015-07-01  | 150 
8  | 62A2806A-4969-417A-B4DF-D547621CC594 | 89.00   | 2016-03-10  | 0 
8  | 1A2D780D-8C11-451C-8A64-6D49140B6232 | 84.00   | 2016-08-05  | 148 

Ich benutze Sql Server 2012, Sql Server Management Studio Version 11.0.5058.0 Vielen Dank.

Antwort

2

Versuchen Sie es.

;WITH patient_results 
AS 
( 
    SELECT * FROM 
     (VALUES (1, 'EA11AEE3-1D90-4602-9A37-0000007E2293', '85.10' ,'2015-12-11'), 
       (1, '27BCD3E4-2381-4139-B420-0000025B4113', '91.50' ,'2016-01-05'), 
       (1, 'D8969360-45D6-487B-AF94-0000035F78B0', '81.00' ,'2016-07-21'), 
       (5, '446E6413-442A-452A-BCF4-000006AA9896', '58.00' ,'2014-07-01'), 
       (5, '00305129-BC14-4A12-8368-00000AC04A9B', '53.00' ,'2014-12-13'), 
       (5, '96A67E53-2D6C-430B-A01F-00000AE4C37B', '42.80' ,'2015-02-01'), 
       (5, '7C330511-3E99-488C-AF5E-00000BDFA3FF', '54.00' ,'2015-07-01'), 
       (8, '62A2806A-4969-417A-B4DF-D547621CC594', '89.00' ,'2016-03-10'), 
       (8, '3B9F4E5B-3433-4F21-850A-FC2127A24B72', '92.60' ,'2016-06-30'), 
       (8, '1A2D780D-8C11-451C-8A64-6D49140B6232', '88.00' ,'2016-08-05')) 
    as t (pat_id, visit_id, result_value, result_date)) 
SELECT *, ISNULL(DATEDIFF(DAY, LAG(result_date) OVER(PARTITION BY pat_id ORDER BY result_date), result_date), 0) as daysBetweenResultDates 
FROM patient_results 
WHERE result_value < 90.00 

Ergebnis

pat_id    visit_id     result_value result_date DaysBetweenResultDates 
1  EA11AEE3-1D90-4602-9A37-0000007E2293 85.10   2015-12-11  0 
1  D8969360-45D6-487B-AF94-0000035F78B0 81.00   2016-07-21  223 
5  446E6413-442A-452A-BCF4-000006AA9896 58.00   2014-07-01  0 
5  00305129-BC14-4A12-8368-00000AC04A9B 53.00   2014-12-13  165 
5  96A67E53-2D6C-430B-A01F-00000AE4C37B 42.80   2015-02-01  50 
5  7C330511-3E99-488C-AF5E-00000BDFA3FF 54.00   2015-07-01  150 
8  62A2806A-4969-417A-B4DF-D547621CC594 89.00   2016-03-10  0 
8  1A2D780D-8C11-451C-8A64-6D49140B6232 88.00   2016-08-05  148 
0

können Sie OUTER vorherigen Werte verwenden GILT zu erhalten:

SELECT p.*, 
     ISNULL(DATEDIFF(DAY,t.result_date,p.result_date),0) AS DaysBetweenResultDates 
FROM patient_results p 
OUTER APPLY (
    SELECT TOP 1 result_date 
    FROM patient_results 
    WHERE pat_id = p.pat_id and 
      result_date < p.result_date and 
      result_value < 90 
    ORDER BY result_date DESC) as t 
WHERE p.result_value <90 
Verwandte Themen