从table1 里获取每个person_id最大end_date的数据。
select fmit.* from table1 fmit
INNER JOIN (
select person_id pi,max(end_date) med
from table1
group by person_id
)fmi on fmit.person_id=fmi.pi and fmit.end_date=fmi.med
下面这个不行,因为下面这个sql,如果end_date相同,对于每个person_id只能获取一条数据。而不能把end_date为最大日期的都拿出来。
SELECT *,MAX(end_date) from table1 GROUP BY person_id
比如
person_id param end_date
111 a 2023-09-26
111 b 2023-09-26
111 b 2023-09-02
222 a 2023-09-01
第一个sql取出的结果是:
person_id param end_date
111 a 2023-09-26
111 b 2023-09-26
222 a 2023-09-01
而第二个sql取出的结果是:
person_id param end_date
111 a 2023-09-26
222 a 2023-09-01