【问题标题】:Merge rows into jsonb object将行合并到 jsonb 对象中
【发布时间】:2021-01-19 08:07:50
【问题描述】:

(这是Postgresql - Count freuency of array or jsonb object之后的后续问题)

postgresql 12+,给定以下输入行:

expected 输出为:

uid   tag_freq

1     {'a':2, 'b':1, 'c':1}
2     {'a':1, 'b':2, 'c':2, 'e':1}
...

输出列tag_freq是jsonb对象,是用户的合并结果。

有没有办法写这样的查询?

【问题讨论】:

  • 样本(输入)数据最好显示为formatted text。请参阅here,了解有关如何创建漂亮表格的一些提示。

标签: postgresql jsonb


【解决方案1】:

您可以为此使用jsonb_object_agg()

select uid, jsonb_object_agg(tag, count) as tag_freq
from the_table
group by uid;

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 2016-09-14
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2020-01-16
    相关资源
    最近更新 更多