postgresql - 如何返回分组 user_id 的最新值?

标签 postgresql

我怎样才能使结果只返回每个分组 user_id 的最后一个条目。示例如下。

此外,我们将不胜感激任何改进查询的方法。 properties 是一个 hstore 列。

SELECT user_id, json_agg(to_json(properties -> 'following')
ORDER BY id DESC) AS followings,
json_agg(to_json(properties -> 'assumed_gender') ORDER BY id DESC) AS assumed_genders,
json_agg(to_json(properties -> 'email') ORDER BY id DESC) AS emails,
json_agg(to_json(properties -> 'first_name') ORDER BY id DESC) AS first_names,
json_agg(to_json(properties -> 'last_name') ORDER BY id DESC) AS last_names,
json_agg(to_json(properties -> 'country_name') ORDER BY id DESC) AS country_names,
json_agg(to_json(properties -> 'city_name') ORDER BY id DESC) AS city_names,
json_agg(to_json(properties -> 'mobile_number') ORDER BY id DESC) AS mobile_numbers,
json_agg(to_json(properties -> 'submission_url') ORDER BY id DESC) AS submission_urls
FROM "daily_statistics" WHERE "daily_statistics"."campaign_id" = 72 AND "daily_statistics"."metric" = 'participation'
GROUP BY user_id ORDER BY max(id) DESC;

结果:

 user_id |    followings     | assumed_genders |                emails                 |    first_names     |     last_names     |  country_names   |     city_names     |        mobile_number
s        |                         submission_urls
---------+-------------------+-----------------+---------------------------------------+--------------------+--------------------+------------------+--------------------+---------------------
---------+------------------------------------------------------------------
      16 | ["false", "true"] | ["", ""]        | ["lorem@lorem.com", "lorem@amet.com"] | ["dolor", "ipsum"] | ["lorem", "ipsum"] | ["amet", "amet"] | ["dolor", "ipsum"] | ["9707759365", "2572
943441"] | ["http://www.dolor.com/hgtsjcbn", "http://www.sit.com/qlnogrzd"]
(1 row)

期望的结果:

 user_id |    followings     | assumed_genders |                emails                 |    first_names     |     last_names     |  country_names   |     city_names     |        mobile_number
s        |                         submission_urls
---------+-------------------+-----------------+---------------------------------------+--------------------+--------------------+------------------+--------------------+---------------------
---------+------------------------------------------------------------------
      16 | "true"            | ""              | "lorem@amet.com"                      | "ipsum"            | "ipsum"            | "amet"           | "ipsum"            |         "2572
943441"  | "http://www.sit.com/qlnogrzd"
(1 row)

最佳答案

使用distinct on

select distinct on (user_id)
    user_id,
    to_json(properties -> 'following') as followings,
    to_json(properties -> 'assumed_gender') as assumed_genders,
    to_json(properties -> 'email') as emails,
    to_json(properties -> 'first_name') as first_names,
    to_json(properties -> 'last_name') as last_names,
    to_json(properties -> 'country_name') as country_names,
    to_json(properties -> 'city_name') as city_names,
    to_json(properties -> 'mobile_number') as mobile_numbers,
    to_json(properties -> 'submission_url') as submission_urls
from "daily_statistics"
where
    "daily_statistics"."campaign_id" = 72
    and "daily_statistics"."metric" = 'participation'
order by user_id, id desc

http://www.postgresql.org/docs/current/static/sql-select.html

SELECT DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. The DISTINCT ON expressions are interpreted using the same rules as for ORDER BY (see above). Note that the "first row" of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first

关于postgresql - 如何返回分组 user_id 的最新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22981737/

相关文章:

ruby-on-rails - 公寓和设计不创建租户

postgresql - 在 PostGIS 中创建一个空的分块栅格表

postgresql - 检查物化 View 的最后刷新时间

postgresql - 在 PostgreSQL 中选择多个变量

postgresql - 更新后使用触发器更新表

sql - 优化 SQL 查询(查找数据中的差距)Postgresql

sql - 在 postgresql、选项数组或对象中插入 jsonb 数据,有效方式

sql - 在纯 PostgreSQL 中 reshape 查询结果

sql - Postgres jsonb如何检查某些值不为空

postgresql - 在 postgresql 中更新返回顺序