简体   繁体   中英

SQL DB2: Need latest records until column value changes

Need to select all recent records belonging to a client having a unique value in column ANDRI (value = P) until value in column ANDRI changes (value = Q). The records need to be grouped based on client name ID (unique ID) which is present in Table B. Two tables can be joined by column ID which is a PRIMARY KEY.

Here is my DB2 Table A:

+---------+---------------+------+-----+---------+-------+
| ID      | Client Type   | ANDRI|  Date (Timestamp)     |
+---------+---------------+------+-----+---------+-------+
| 878     |  Personal     | P    | 2020-09-22 16:47:08   |
| 576     |  Personal     | P    | 2020-09-22 10:47:08   |
| 745     |  Personal     | P    | 2019-05-21 14:47:08   |
| 142     |  Business     | Q    | 2019-09-20 16:11:08   |
| 711     |  Personal     | Q    | 2018-02-12 15:27:08   |
| 441     |  Personal     | P    | 2018-01-29 10:57:08   |
| 371     |  Personal     | P    | 2017-05-20 11:17:08   |
| 115     |  Personal     | P    | 2016-10-12 14:47:08   |
+---------+---------------+------+-----+---------+-------+

Here is my DB2 Table B:

+---------+---------------+------+-----+---------+-------+
| ID      | Client Name ID| ODER|  Date (Timestamp)     |
+---------+---------------+------+-----+---------+-------+
| 878     |     Alice     | A    | 2020-09-22 16:47:08   |
| 576     |     Alice     | A    | 2020-09-22 10:47:08   |
| 745     |     Alice     | A    | 2019-05-21 14:47:08   |
| 142     |     Sandra    | B    | 2019-09-20 16:11:08   |
| 711     |     Alice     | B    | 2018-02-12 15:27:08   |
| 441     |     Alice     | A    | 2018-01-29 10:57:08   |
| 371     |     Sandra    | A    | 2017-05-20 11:17:08   |
| 115     |     Sandra    | A    | 2016-10-12 14:47:08   |
+---------+---------------+------+-----+---------+-------+

I expect below output:

| ID      | Client Name ID| Date (Timestamp)     |
+---------+---------------+------+-----+---------+
| 878     |  Alice        | 2020-09-22 16:47:08  |
| 576     |  Alice        | 2020-09-22 10:47:08  |
| 745     |  Alice        | 2019-05-21 14:47:08  |

If possible, it would be helpful if we can use ROW_NUMBER() function in SQL.

My SQL which I have been using (but it needs some tweaking):

SELECT T.*
FROM TABLEB T
JOIN 
(
    SELECT Client Name ID 
    FROM ( 
        SELECT Date, Client Name ID
             , ROWNUMBER() OVER (PARTITION BY Client Name ID 
                                 ORDER BY Date DESC) RN
        FROM TABLEB
    )
    WHERE RN IN (1, 2)
    GROUP BY Client Name ID
    HAVING MIN(ODER) = MAX(ODER) AND COUNT(1) = 2 AND MIN(ODER) = 'A'
) G 
    ON G.client name ID = T.client name ID
    AND T.ID IN (SELECT ID FROM TABLEA WHERE ANDRI = 'P')
ORDER BY T.Client Name ID;
select b.*
from a join
     b
     on a.id = b.id
where a.date > (select max(a2.date)
                from a a2
                where a2.andri <> 'P'
               );

Given that the same dates are in both tables, you may not need the join in the outer query:

select b.*
from b
where b.date > (select max(a2.date)
                from a a2
                where a2.andri <> 'P'
               );

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