Postgres 在 json 中找到唯一值
Postgres find unique values in json
我正在使用 Postgresql 并且有一个 table,id,sender::jsonb 和日期,如下所示:
id | sender | last login date
----+-----------------------------------------------------------------------------------+----------------------------------
1 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-10 14:49:36.234504 +00:00
2 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-09 14:49:36.234504 +00:00
3 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-11 14:49:36.234504 +00:00
4 | {"firstName": "Nickolai","lastName": "Nickov", "middleName": "Nikovich", } | 2021-04-30 14:49:36.234504 +00:00
5 | {"firstName": "Nickolai","lastName": "Nickov", "middleName": "Nikovich", } | 2021-04-29 14:49:36.234504 +00:00
6 | {"firstName": "Vladimir","lastName": "Vladimirovich","middleName": "Putout", } | 2021-04-15 14:49:36.234504 +00:00
7 | {"firstName": "Petr", "lastName": "Petrov", "middleName": "Petrovich", } | 2021-04-10 14:49:36.234504 +00:00
8 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-01 14:49:36.234504 +00:00
9 | {"firstName": "Ignat", "lastName": "Ignatov", "middleName": "Ignatovich", }| 2021-04-06 14:49:36.234504 +00:00
10| {"firstName": "Vladimir","lastName": "Vladimirovich","middleName": "Putout", } | 2021-04-17 14:49:36.234504 +00:00
11| {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-12 14:49:36.234504 +00:00
p.s.There可能是“发件人”栏中的其他信息,但搜索唯一性只需要通过“firstName”、“lastName”、“middleName”
有必要 return 一个由唯一名称和最新日期组成的结果。特别是,我想得到结果:
id | sender | last login date
----+-----------------------------------------------------------------------------------+----------------------------------
4 | {"firstName": "Nickolai","lastName": "Nickov", "middleName": "Nikovich", } | 2021-04-30 14:49:36.234504 +00:00
10| {"firstName": "Vladimir","lastName": "Vladimirovich","middleName": "Putout", } | 2021-04-17 14:49:36.234504 +00:00
11| {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-12 14:49:36.234504 +00:00
7 | {"firstName": "Petr", "lastName": "Petrov", "middleName": "Petrovich", } | 2021-04-10 14:49:36.234504 +00:00
9 | {"firstName": "Ignat", "lastName": "Ignatov", "middleName": "Ignatovich", }| 2021-04-06 14:49:36.234504 +00:00
由于使用了 json,一切都变得非常复杂。我想做 - “名称”连接并执行分组和排序,但不幸的是它不起作用。
您可以使用 distinct on()
来执行此操作:
select distinct on (firstname, lastname) id, sender, last_login_date
from (
select id, sender, last_login_date,
sender ->> 'firstName' as firstname,
sender ->> 'lastName' as lastname
from the_table
) t
order by firstname, lastname, last_login_date desc
您可以使用 window 函数来完成:
select * from
(
select * ,rank() over (partition by sender->> 'firstName',sender->> 'lastName' order by last_login_date desc) rn
from yourtable
) t
where rn = 1
order by last_login_date desc
db<>fiddle here
我正在使用 Postgresql 并且有一个 table,id,sender::jsonb 和日期,如下所示:
id | sender | last login date
----+-----------------------------------------------------------------------------------+----------------------------------
1 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-10 14:49:36.234504 +00:00
2 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-09 14:49:36.234504 +00:00
3 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-11 14:49:36.234504 +00:00
4 | {"firstName": "Nickolai","lastName": "Nickov", "middleName": "Nikovich", } | 2021-04-30 14:49:36.234504 +00:00
5 | {"firstName": "Nickolai","lastName": "Nickov", "middleName": "Nikovich", } | 2021-04-29 14:49:36.234504 +00:00
6 | {"firstName": "Vladimir","lastName": "Vladimirovich","middleName": "Putout", } | 2021-04-15 14:49:36.234504 +00:00
7 | {"firstName": "Petr", "lastName": "Petrov", "middleName": "Petrovich", } | 2021-04-10 14:49:36.234504 +00:00
8 | {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-01 14:49:36.234504 +00:00
9 | {"firstName": "Ignat", "lastName": "Ignatov", "middleName": "Ignatovich", }| 2021-04-06 14:49:36.234504 +00:00
10| {"firstName": "Vladimir","lastName": "Vladimirovich","middleName": "Putout", } | 2021-04-17 14:49:36.234504 +00:00
11| {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-12 14:49:36.234504 +00:00
p.s.There可能是“发件人”栏中的其他信息,但搜索唯一性只需要通过“firstName”、“lastName”、“middleName”
有必要 return 一个由唯一名称和最新日期组成的结果。特别是,我想得到结果:
id | sender | last login date
----+-----------------------------------------------------------------------------------+----------------------------------
4 | {"firstName": "Nickolai","lastName": "Nickov", "middleName": "Nikovich", } | 2021-04-30 14:49:36.234504 +00:00
10| {"firstName": "Vladimir","lastName": "Vladimirovich","middleName": "Putout", } | 2021-04-17 14:49:36.234504 +00:00
11| {"firstName": "Ivan", "lastName": "Ivanov", "middleName": "Ivanovich", } | 2021-04-12 14:49:36.234504 +00:00
7 | {"firstName": "Petr", "lastName": "Petrov", "middleName": "Petrovich", } | 2021-04-10 14:49:36.234504 +00:00
9 | {"firstName": "Ignat", "lastName": "Ignatov", "middleName": "Ignatovich", }| 2021-04-06 14:49:36.234504 +00:00
由于使用了 json,一切都变得非常复杂。我想做 - “名称”连接并执行分组和排序,但不幸的是它不起作用。
您可以使用 distinct on()
来执行此操作:
select distinct on (firstname, lastname) id, sender, last_login_date
from (
select id, sender, last_login_date,
sender ->> 'firstName' as firstname,
sender ->> 'lastName' as lastname
from the_table
) t
order by firstname, lastname, last_login_date desc
您可以使用 window 函数来完成:
select * from
(
select * ,rank() over (partition by sender->> 'firstName',sender->> 'lastName' order by last_login_date desc) rn
from yourtable
) t
where rn = 1
order by last_login_date desc
db<>fiddle here