简体   繁体   English

SQL DB2:需要最新记录,直到列值更改

[英]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).需要选择属于在 ANDRI 列中具有唯一值(值 = P)的客户的所有最近记录,直到 ANDRI 列中的值发生变化(值 = 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.记录需要根据表 B 中存在的客户端名称 ID(唯一 ID)进行分组。两个表可以通过作为主键的列 ID 连接。

Here is my DB2 Table A:这是我的 DB2 表 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:这是我的 DB2 表 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.如果可能的话,如果我们可以在 SQL 中使用 ROW_NUMBER() 函数会很有帮助。

My SQL which I have been using (but it needs some tweaking):我一直在使用的 SQL(但需要一些调整):

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:鉴于同一日期两个表中,你可能不需要join外部查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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