如何获得每个单独pat_id的结果日期之间的天数?

时间:2016-08-08 15:24:08

标签: sql-server tsql sql-server-2012

我希望获得每位患者的结果日期之间的日期:仅查看结果值<90.00的结果日期

;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))

基于以上看起来像这样:

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

我使用的是Sql Server 2012,Sql server management studio版本11.0.5058.0 谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个。

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

<强>结果

Dim strMPG As String
Dim strBusinessNames As String
Dim strCustomerNumbers As String
Dim strCustomerNames As String
Dim strCountStartDates As String
Dim strCCStatus As String

答案 1 :(得分:0)

您可以使用OUTER APPLY获取以前的值:

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