如果根据日期提取连接上的任何重复列,则连接表以查找不同的行



我有两个表,我需要连接这些表有可能连接的表可能返回重复的行但有列更新日期将是唯一的所以我需要从这些表中获取记录并从第二个表中获取不同的记录

表1

<表类> Id AccountKey tbody><<tr>112213

假设我们按id和accountKey进行分组下面的查询将得到您想要的结果。

SELECT a.id, a.accountKey, MAX(b.cdate)
FROM table1 a
JOIN table2 b ON a.accountKey = b.accountKey
GROUP BY a.id, a.accountKey

试试这个

drop table if exists one;
drop table if exists two;
create table one
(
ID         [Int]
, AccountKey [Int]
)
;
insert into one
values (1, 12)
, (2, 13)
;
create table two
(
Rolekey    [Int]
, AccountKey [Int]
, Date       [Date]
)
;
insert into two
values (1, 12, '2022-12-02')
, (2, 12, '2022-12-01')
, (3, 13, '2022-12-01')
;
select one.id
, one.AccountKey
, max(Date) as Date
from one, two
where one.AccountKey = two.AccountKey
group by one.id
, one.AccountKey
;

最新更新