如何在SQL中使用上一行中的值

时间:2017-08-30 05:47:21

标签: mysql sql

以下SQL Query提供了如下所述的输出:

SELECT claim_id,person_id,service_date,readmission_in_60_days 
FROM xyz WHERE person_id IN("00026cb6","021fb6bd") GROUP BY person_id,service_date
ORDER BY service_date ASC



 claim_id   person_id   service_date  readmission_in_60_days
    8       021fb6bd    2015-01-01             NULL
    304     021fb6bd    2015-01-05             NULL
    296     021fb6bd    2015-01-06             NULL
    11888   021fb6bd    2015-01-07             NULL
    23928   021fb6bd    2015-01-08             NULL
    265442  00026cb6    2016-07-20             NULL
    518062  00026cb6    2016-09-28             NULL
    579739  00026cb6    2016-10-02             NULL

这里我想检查特定person_id的n和n-1行service_date。如果n和n-1 service_date之间的差异小于60天,那么n row列readmission_in_60_days =“TRUE”否则为“FALSE”。< / p>

所以SQL查询的输出应该是:

claim_id   person_id   service_date  readmission_in_60_days
    8       021fb6bd    2015-01-01             FALSE
    304     021fb6bd    2015-01-05             TRUE
    296     021fb6bd    2015-01-06             TRUE
    11888   021fb6bd    2015-01-07             TRUE
    23928   021fb6bd    2015-01-08             TRUE
    265442  00026cb6    2016-07-20             FALSE
    518062  00026cb6    2016-09-28             FALSE
    579739  00026cb6    2016-10-02             TRUE

3 个答案:

答案 0 :(得分:5)

请试试这个。

Declare @tblTest as table
(
  claim_id   int,
  person_id  varchar(50),
  service_date  datetime,
  readmission_in_60_days varchar(10) null
)

insert into @tblTest values (304   , '021fb6bd','2015-01-05', NULL)
insert into @tblTest values (296   , '021fb6bd','2015-01-06', NULL)
insert into @tblTest values (11888 , '021fb6bd','2015-01-07', NULL)
insert into @tblTest values (23928 , '021fb6bd','2015-01-08', NULL)
insert into @tblTest values (265442, '00026cb6','2016-07-20', NULL)
insert into @tblTest values (518062, '00026cb6','2016-09-28', NULL)
insert into @tblTest values (579739, '00026cb6','2016-10-02', NULL)

SELECT t.claim_id, 
    t.person_id, 
    t.service_date,
    t.PreviousDate,
    DATEDIFF(DAY, t.service_date, t.PreviousDate) AS TotalDiffDays,
    CASE WHEN ISNULL(t.PreviousDate,'') = '' THEN 'FALSE' 
         WHEN DATEDIFF(DAY, t.PreviousDate, t.service_date) > 60 THEN 'FALSE'
         WHEN DATEDIFF(DAY, t.PreviousDate, t.service_date) < 60 THEN 'TRUE' 
         END AS readmission_in_60_days
FROM
    (SELECT 
        claim_id, 
        person_id, 
        service_date,
        LAG(service_date, 1, null) OVER (ORDER BY claim_id ASC) AS PreviousDate from @tblTest
    ) t

答案 1 :(得分:4)

您可以使用EXISTS和子查询:

SELECT person_id, service_date,
       (EXISTS (SELECT 1
                FROM xyz xyz2
                WHERE xyz2.person_id = xyz.personid AND
                      xyz2.service_date < xyz.service_date AND
                      xyz2.service_date >= DATE_SUB(xyz.service_date, INTERVAL 60 DAY)
       ) as readmission_in_60_days 
FROM xyz
WHERE person_id IN ('00026cb6', '021fb6bd')
GROUP BY person_id, service_date
ORDER BY service_date ASC

注意:我删除了claim_id。如果您想将其包含在SELECT中,则可以:

  • 将其添加到GROUP BY
  • 使用汇总功能,例如MIN()MAX()GROUP_CONCAT()

答案 2 :(得分:1)

另一种方法是通过变量模拟MySQL中的LAG函数。像这样:

SET @prevdate = '1900-01-01';
SET @prevperson = "";

SELECT id, personid, servicedate, 
CASE WHEN prevperson = personid THEN
    CASE WHEN datediff(servicedate,prevdate) > 60 THEN FALSE
    ELSE TRUE END
    ELSE FALSE END AS readmission_within_60_days
FROM
(SELECT id, personid, servicedate,
@prevdate prevdate,
@prevperson prevperson,
@prevdate := servicedate as thisdate, 
@prevperson := personid as thisperson FROM xyz
ORDER BY id, personid) t order by id, personid; 

修改

如果你不能使用变量,你可以得到你想要的:

SELECT z.id, 
       z.personid, 
       z.servicedate, 
       CASE WHEN z.MaxPrevDate IS NULL 
           THEN FALSE 
       ELSE 
          CASE WHEN datediff(z.servicedate,z.maxprevdate) > 60 
              THEN FALSE
          ELSE 
              TRUE 
          END 
       END as readmission_within_60_days 
FROM
    (SELECT x.id, x.personid, x.servicedate, 
    (SELECT Max(servicedate) FROM xyz y 
     WHERE y.personid = x.personid AND y.id < x.id) AS maxPrevDate 
     FROM xyz x) z