gpt4 book ai didi

json - PostgreSQL:如何从 GROUP BY ROLLUP 数据生成 JSON

转载 作者:行者123 更新时间:2023-11-29 13:42:35 24 4
gpt4 key购买 nike

我需要一些帮助来将 Group By ROLLUP(...) 聚合到嵌套的 JSON 中。

我的查询(结果)如下所示


查询#1

SELECT main, sub, subsub, count(*) FROM test 
GROUP BY ROLLUP(main, sub, subsub)
ORDER BY main, sub, subsub;

结果

| main | sub | subsub | count |
| ---- | --- | ------ | ----- |
| c | c-1 | c-1-1 | 1 |
| c | c-1 | c-1-2 | 1 |
| c | c-1 | | 2 |
| c | | | 2 |
| d | d-1 | d-1-1 | 1 |
| d | d-1 | | 1 |
| d | | | 1 |
| | | | 3 |

但我希望它的结果像下面这样在 json 中

{
c: {
'total': 2,
c-1: {
'total': 2,
'c-1-1': 1,
'c-1-2': 1,
}
},

d: {
'total': 1,
'd-1': {
'total': 1,
'd-1-1': 1
}
}
}

我已经尝试过 json_build_object 等,但我无法操作 ROLLUP 数据。非常感谢任何帮助!

这是一个 link to the fiddle (PostgreSQL V10)

最佳答案

你需要一个分层查询:

with totals as (
select main, sub, subsub, count(*)
from test
group by rollup(main, sub, subsub)
order by main, sub, subsub
)

select jsonb_object_agg(main, sub) as main
from (
select
main,
jsonb_object_agg(
coalesce(sub, 'total'),
case when sub is null
then subsub->'total'
else subsub end
) as sub
from (
select
main, sub,
jsonb_object_agg(
coalesce(subsub, 'total'), count
) as subsub
from totals
group by main, sub
having main is not null
) s
group by main
) m
where main is not null

db-fiddle.

没有cte的版本:

select jsonb_object_agg(main, sub) as main
from (
select
main,
jsonb_object_agg(
coalesce(sub, 'total'),
case when sub is null
then subsub->'total'
else subsub end
) as sub
from (
select
main, sub,
jsonb_object_agg(
coalesce(subsub, 'total'),
count
) as subsub
from (
select main, sub, subsub, count(*)
from test
group by rollup(main, sub, subsub)
) subsub
group by main, sub
having main is not null
) sub
group by main
) main
where main is not null

关于json - PostgreSQL:如何从 GROUP BY ROLLUP 数据生成 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com