简体   繁体   中英

How to get last modified row with condition

I have below mentioned tables:

Table1:

ID        Unique_Key      Date                     MapId
II-10     RTE-011         2018-04-07 15:18:25      er-12-tt
II-11     RTE-011         2018-05-17 11:44:47      er-13-td
II-12     RTE-011         2018-06-12 13:22:29      ee-13-rt
II-13     RTE-012         2018-04-07 19:17:14      eg-15-st
II-14     RTE-012         2018-04-07 21:22:37      hr-15-yt
II-15     RTE-013         2018-07-07 20:27:38      tr-16-yt
II-15     RTE-013         2018-08-07 14:18:33      tr-16-yt

Table2: 

Id              Status
er-12-tt        Open
er-13-td        Closed
er-13-rt        Closed
eg-15-st        Closed
hr-15-yt        Pending
tr-16-yt        Open
tr-16-yt        Pending

By using above mentioned both the tables, I want to fetch latest row of only those records which has status as Closed .

We need to exclude those Unique_Key against which any value is not equlal to Closed

Required Output would be:

ID        Unique_Key      Date                     MapId          Id         Status
II-12     RTE-011         2018-06-12 13:22:29      ee-13-rt       er-13-rt        Closed
II-13     RTE-012         2018-04-07 19:17:14      eg-15-st       eg-15-st        Closed    

We can take a common approach here, and join to a subquery to do the filtering:

SELECT
    t1.ID, t1.Unique_Key, t1.Date, t1.MapId, t2.Id, t2.Status
FROM Table1 t1
INNER JOIN Table2 t2
    ON t1.MapId = t2.Id
INNER JOIN
(
    SELECT t1.Unique_Key, MAX(t1.Date) AS max_date
    FROM Table1 t1
    INNER JOIN Table2 t2
        ON t1.MapId = t2.Id
    WHERE t2.Status = 'Closed'
    GROUP BY t1.Unique_Key
) t3
    ON t1.Unique_Key = t3.Unique_Key AND t1.Date = t3.max_date;

在此处输入图片说明

Demo

The join to the subquery aliased as t3 filters off all records for each unique key except for the most recent one which has a closed status.

For posterity's sake, there is another approach to this question using analytic functions. Given that MySQL 8+ will soon be the standard, it makes sense to consider this option as well:

SELECT
    ID_t1, Unique_Key, Date, MapId, ID_t2, Status
FROM
(
    SELECT t1.ID AS ID_t1, t1.Unique_Key, t1.Date, t1.MapId, t2.Id AS ID_t2,
        t2.Status,
        ROW_NUMBER() OVER (PARTITION BY t1.Unique_Key ORDER BY t1.Date DESC) rn
    FROM Table1 t1
    INNER JOIN Table2 t2
        ON t1.MapId = t2.Id
    WHERE t2.Status = 'Closed'
) t
WHERE rn = 1;

Demo

Firstly, in a subquery, we determine the maximum date for a group of Unique_Key . We filter out those Unique_Key values which has atleast one row with Status = Closed , using HAVING SUM(t2.Status = Closed) > 0

The result set of this sub-query is then utilized as a Derived Table . It is then joined back to Table1 and Table2 , and we filter out those cases where maximum status is Closed , using WHERE t22.Status = 'Closed'

Try:

SELECT t11.ID, 
       t11.Unique_Key, 
       t11.Date, 
       t11.MapId, 
       t22.Id, 
       t22.Status 
FROM Table1 AS t11 
JOIN Table2 AS t22 ON t22.Id = t11.MapId 
JOIN (
       SELECT t1.Unique_Key, 
              MAX(t1.Date) AS max_Date 
       FROM Table1 AS t1 
       JOIN Table2 AS t2 ON t2.Id = t1.MapId 
       GROUP BY t1.Unique_Key 
       HAVING SUM(t2.Status = Closed) > 0 
     ) AS dt ON dt.Unique_Key = t11.Unique_Key AND 
                dt.max_Date = t11.Date 
WHERE t22.Status = 'Closed'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM