[英]Postgres Creating JSON Object from Aggregated Rows
我在創建一個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對象。
只需使用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
考慮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"]}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.