SQL Select 一列的唯一值和另一列的最新值
SQL Select unique values by one column with the latest value by another column
我有以下 table 结构:
leads:
name | email | date
-------------------------------------------------------
John | john@mail.com | 2020-01-21
J | john@mail.com | 2020-01-20
Alex | alex@mail.com | 2020-01-19
A | alex@mail.com | 2020-01-18
James | james@mail.com | 2020-01-17
我只需要 select 具有唯一电子邮件和最新关联名称的行,因此预期结果是:
name | email | date
-------------------------------------------------------
John | john@mail.com | 2020-01-21
Alex | alex@mail.com | 2020-01-19
James | james@mail.com | 2020-01-17
使用不存在
select * from tbl a
where not exist(select 1 from tbl b where a.email = b.email and a.date < b.date)
我有以下 table 结构:
leads:
name | email | date
-------------------------------------------------------
John | john@mail.com | 2020-01-21
J | john@mail.com | 2020-01-20
Alex | alex@mail.com | 2020-01-19
A | alex@mail.com | 2020-01-18
James | james@mail.com | 2020-01-17
我只需要 select 具有唯一电子邮件和最新关联名称的行,因此预期结果是:
name | email | date
-------------------------------------------------------
John | john@mail.com | 2020-01-21
Alex | alex@mail.com | 2020-01-19
James | james@mail.com | 2020-01-17
使用不存在
select * from tbl a
where not exist(select 1 from tbl b where a.email = b.email and a.date < b.date)