Postgres从聚合行创建JSON对象

时间:2017-11-07 18:30:27

标签: sql postgresql

我在创建JSON对象时遇到了一些问题,其中对象的键是我在Postgres中聚合行的值。

以下是我正在使用的表格:

create table if not exists safety_training_options (
  id serial primary key,
  option_type text not null,
  name text not null
)

以及一些示例数据:

insert into safety_training_options (option_type, name)
values ('category', 'General Industry'),
       ('category', 'Maritime'),
       ('category', 'Construction'),
       ('frequency', 'Daily'),
       ('frequency', 'Weekly'),
       ('frequency', 'Bi-weekly'),
       ('method', 'Online'),
       ('method', 'Classroom');

到目前为止,这是我的查询,它将为我提供汇总的行:

select 
    option_type as type,
    json_agg(sto.name) as options
from safety_training_options as sto
group by sto.option_type;

结果集:

╔════════════╦═════════════════════════╗
║    type    ║         options         ║
╠════════════╬═════════════════════════╣
║ method     ║ ["Online", "Classroom"] ║
║ frequency  ║ ["Daily, "Weekly", ...] ║
║ class_type ║ [...]                   ║
║ category   ║ [...]                   ║
╚════════════╩═════════════════════════╝

我遇到的问题是如何构建一个json对象,其中键是type列中的值,值是options列中的数组。我希望我的最终结果如下:

{
    "method": [...],
    "category": [...],
    "frequency": [...],
    "class_type": [...]
}

奖励问题是我可以重命名这些值以使它们复数化吗?如果我可以将json对象中的键复用为"方法"那将是很好的。 "分类" "频率"和" class_types"。我知道我可以将表中的值更改为复数,但我很好奇是否有另一种方法可以构建自定义json对象。

2 个答案:

答案 0 :(得分:3)

只需使用json_object_agg

WITH tmp AS (
    SELECT 
        option_type,
        json_agg(sto.name) as training_options
    FROM 
        safety_training_options as sto
    GROUP BY 
        sto.option_type
)
SELECT json_object_agg(option_type, training_options) FROM tmp

答案 1 :(得分:1)

使用条件row_to_json来考虑array_agg

SELECT row_to_json(r) as output
FROM 
(  
   ( SELECT array_remove(array_agg(CASE WHEN s.option_type = 'category' 
                                        THEN s.name ELSE NULL END), NULL) AS category,
            array_remove(array_agg(CASE WHEN s.option_type = 'frequency' 
                                        THEN s.name ELSE NULL END), NULL) AS frequency,
            array_remove(array_agg(CASE WHEN s.option_type = 'method' 
                                        THEN s.name ELSE NULL END), NULL) AS method
     FROM safety_training_options s
    )      
) r;

-- {"category":["General Industry","Maritime","Construction"],
--  "frequency":["Daily","Weekly","Bi-weekly"],
--  "method":["Online","Classroom"]}

Rextester Demo