选择时间戳单元格上的最新条目
Selecting the most recent entry on a timestamp cell
我有这两个表:
User:
=======================
id | Name | Email
=======================
1 | User-A| a@mail
2 | User-B| b@mail
=======================
Entry:
=================================================
id | agree | createdOn | userId
=================================================
1 | true | 2020-11-10 19:22:23 | 1
2 | false | 2020-11-10 22:22:23 | 1
3 | true | 2020-11-11 12:22:23 | 1
4 | true | 2020-11-04 22:22:23 | 2
5 | false | 2020-11-12 02:22:23 | 2
================================================
我需要得到以下结果:
=============================================================
Name | Email | agree | createdOn
=============================================================
User-A | a@mail | true | 2020-11-11 22:22:23
User-B | b@mail | false | 2020-11-12 02:22:23
=============================================================
我 运行 的 Postgres 查询是:
select distinct on (e."createdOn", u.id)
u.id , e.id ,u."Name" , u.email, e.agree, e."createdOn" from "user" u
inner join public.entry e on u."id" = e."userId"
order by "createdOn" desc
但问题是它returns所有条目在做join之后!我只想要 createdOn
单元格的最新条目。
您想要每个用户的最新条目。为此,您需要 distinct on
子句中的用户 ID,而不是其他列。这保证每个用户在结果集中一行。
然后,您需要将该列放在 order by
子句的最前面,然后是 createdOn desc
。这打破了联系并决定哪一行将保留在每组中:
select distinct on (u.id) u.id , e.id ,u."Name" , u.email, e.agree, e."createdOn"
from "user" u
inner join public.entry e on u."id" = e."userId"
order by u.id, "createdOn" desc
您还可以使用 row_number 到 select 最新行,然后进行连接
SELECT * FROM USER A
LEFT JOIN (
SELECT * FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY USERID ORDER BY CREATEDON DESC) AS RN
FROM ENTRY
) K WHERE RN = 1
) B
ON A.ID = B.USERID
尝试使用 createdon = max(createdon) 函数。按用户分组
我有这两个表:
User:
=======================
id | Name | Email
=======================
1 | User-A| a@mail
2 | User-B| b@mail
=======================
Entry:
=================================================
id | agree | createdOn | userId
=================================================
1 | true | 2020-11-10 19:22:23 | 1
2 | false | 2020-11-10 22:22:23 | 1
3 | true | 2020-11-11 12:22:23 | 1
4 | true | 2020-11-04 22:22:23 | 2
5 | false | 2020-11-12 02:22:23 | 2
================================================
我需要得到以下结果:
=============================================================
Name | Email | agree | createdOn
=============================================================
User-A | a@mail | true | 2020-11-11 22:22:23
User-B | b@mail | false | 2020-11-12 02:22:23
=============================================================
我 运行 的 Postgres 查询是:
select distinct on (e."createdOn", u.id)
u.id , e.id ,u."Name" , u.email, e.agree, e."createdOn" from "user" u
inner join public.entry e on u."id" = e."userId"
order by "createdOn" desc
但问题是它returns所有条目在做join之后!我只想要 createdOn
单元格的最新条目。
您想要每个用户的最新条目。为此,您需要 distinct on
子句中的用户 ID,而不是其他列。这保证每个用户在结果集中一行。
然后,您需要将该列放在 order by
子句的最前面,然后是 createdOn desc
。这打破了联系并决定哪一行将保留在每组中:
select distinct on (u.id) u.id , e.id ,u."Name" , u.email, e.agree, e."createdOn"
from "user" u
inner join public.entry e on u."id" = e."userId"
order by u.id, "createdOn" desc
您还可以使用 row_number 到 select 最新行,然后进行连接
SELECT * FROM USER A
LEFT JOIN (
SELECT * FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY USERID ORDER BY CREATEDON DESC) AS RN
FROM ENTRY
) K WHERE RN = 1
) B
ON A.ID = B.USERID
尝试使用 createdon = max(createdon) 函数。按用户分组