json - 在postgres中分组json对象

标签 json postgresql postgresql-9.4

我正在使用 select 插入到 postgres 中。基本上是转成json类型。

SELECT DISTINCT  ON (name, number)
  JSON_BUILD_OBJECT(name, JSON_BUILD_OBJECT(phone_number_of, number))
FROM table1 t1, tale2 t2, table3 t3
WHERE
  t1.customer_fk_id = t2.id
AND t1.id = t3.proposal_customer_id
ORDER BY name, number, priority DESC

我正在获取输出:

{"ALIP KUMAR" : {"Mobile" : "8*******"}}
{"ALIP KUMAR" : {"Residence" : "9******"}}
{"Abdul Gaffar" : {"Office" : "9*******"}}
{"Abdul Khalique" : {"Mobile" : "98*****"}}
{"Abdul Khalique" : {"Mobile" : "97*****"}}

有没有什么方法可以对数据进行分组,使一个名字有一个包含所有键的键,即手机、住所等... 如果说住所有多个号码,则将其放入列表中。 输出应该是

{"ALIP KUMAR" : {"Mobile" : "8*******"}
                 "Residence" : "9******"}}
{"Abdul Gaffar" : {"Office" : "9*******"}}
{"Abdul Khalique" : {"Mobile" : ["98*****", "97*****]}}

最佳答案

有两个聚合函数应该做你想做的事,除了存储数字而不是数组,如果它只有一个元素。

首先,您需要使用以下方法聚合数字:json_agg(number),按name, phone_number_of 分组>

然后,如果您需要将单个电话号码作为数字/字符串而不是数组,则使用 json_array_length(json) 检查长度,如果它小于 2,则使用 获取第一个元素json->>0 并使用它代替数组。

一旦你有了 name, phone_number_of, number_array_or_value 然后按 name 将它们分组并使用函数 json_object_agg(key, value) 聚合>

此处描述了这些聚合函数:https://www.postgresql.org/docs/9.4/functions-aggregate.html

例子:

select json_object_agg(name, phones)
  from (
        select name,
               json_object_agg(typ, case when json_array_length(numbers) < 2 then numbers->0 else numbers end) as phones
          from (
                select name, typ, json_agg(number) as numbers
                  from (
                        select 'name1' as name, 'work' as typ, '34636432' as number
                        union all
                        select 'name1' as name, 'mobile' as typ, '12453435' as number
                        union all
                        select 'name1' as name, 'mobile' as typ, '654745' as number
                        union all
                        select 'name2' as name, 'home' as typ, '8643534434' as number
                        union all
                        select 'name3' as name, 'work' as typ, '24778457' as number
                       ) AS sub1
                 group by name, typ
               ) AS sub2
         group by name
      ) as sub3
 group by name;

                             json_object_agg
--------------------------------------------------------------------------
 { "name2" : { "home" : "8643534434" } }
 { "name3" : { "work" : "24778457" } }
 { "name1" : { "work" : "34636432", "mobile" : ["12453435", "654745"] } }
(3 rows)

关于json - 在postgres中分组json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56641614/

相关文章:

postgresql - 如何识别禁用真空的表

objective-c - 网络请求失败 - NSURLSession - App Sandbox - Xcode 9

android - org.json.JSONException : Value 10. 07526 at 0 of type java.lang.Double cannot be converted to JSONObject

arrays - 使用 JPA 1.0 在 NetBeans 主/细节示例表单中显示可变 PostgreSQL 数组

sql - 交叉表查询提供了意外的结果

ruby-on-rails - 原始 SQL 请求中的 Rails 4 字符串插值

arrays - 如何在 Postgresql 中获取数组的 JavaScript/JSON 数组?

ios - 快速解析从 API 返回的 JSON 对象

javascript - jQGrid - 更改列的顺序

PostgreSQL 9.4 - 在 EXCLUDE 约束中使用自定义运算符