需求:POSTGRESQL group by 取最新一条或者多条数据
SELECT * from
(
SELECT
t.create_time,
t.user_id,
t.seller_activity_id,
t.id,
RANK() OVER(PARTITION BY T.seller_activity_id ORDER BY T.create_time DESC) RK
FROM seller_activity_registration T
WHERE T.user_id = 'XXX'
) as A
where A.RK < 2
user_id 为用户
seller_activity_id 为业务ID,我这里是PARTITION BY T.seller_activity_id就是按这个业务分组
ORDER BY T.create_time DESC 是按创建时间排序
where A.RK < 2 和where A.RK = 1意思一样,选取记录的数量