Postgres SQL:在单个表上构建嵌套的json结构?

时间:2018-10-31 12:37:39

标签: sql json postgresql

我正在尝试从单个表构建嵌套的Json结构。

我试图使最终结果看起来像这样:

[
    {
        "contract_number":"GS00F0XXX",
        'category': [
            {
                'cat_id': "874 1",
                'socio': {
                    "eighta":false,
                    "sdvosb":false,
                    "edwosb":false
                }   
            },
            {
                "cat_id":"874 6",
                'socio': {
                    "eighta":false,
                    "sdvosb":false,
                    "edwosb":false,
                }
            }
        ]
    },
    {
        'contract_number': "GS00Q14OAXXX",
        'category': [
            {
                'cat_id': 'OASIS POOL1',
                'socio': ...
            }
        ]
    }  
]

基础表如下所示:

contract_type   contract_number duns_number eighta  sdvosb  edwosb  category
MOBIS           GS00F0XXX       5555555     FALSE   FALSE   FALSE   874 1
MOBIS           GS00F0XXX       5555555     FALSE   FALSE   FALSE   874 6
MOBIS           GS00F0XXX       5555555     FALSE   FALSE   FALSE   874 7
OASIS           GS00Q14OAXXX    5555555     FALSE   FALSE   FALSE   OASIS POOL1
OASIS           GS00Q14OAXXX    5555555     FALSE   FALSE   FALSE   OASIS POOL2

我已经尝试过该查询,但是它将所有内容都放在两行中,并且每次我尝试嵌套json_agg调用时,都会出现有关nestng聚合函数的错误。

select 
    json_build_object(
        'contract_number', contract_number,
        'info', json_agg(
            json_build_object(
                    'category_id', category,
                    'eighta',eighta,
                    'sdvosb',sdvosb,
                    'edwosb',edwosb
                )
        )
    )
from contract_vehicle
group by duns_number, contract_number

这将导致SQL Error [42803]: ERROR: aggregate function calls cannot be nested

select 
    json_build_object(
        'contract_number', contract_number,
        'info', json_agg(
            json_build_object(
                    'category_id', category,
                    'socio', jsonb_agg(
                        json_build_object(
                            'eighta',eighta,
                            'sdvosb',sdvosb,
                            'edwosb',edwosb
                        )
                    )
                )
        )
    )
from contract_vehicle
group by duns_number, contract_number 

1 个答案:

答案 0 :(得分:0)

我不确定是否可以,但是请尝试一下:)

with cte as (
    select  json_agg(
                        json_build_object(
                            'eighta',eighta,
                            'sdvosb',sdvosb,
                            'edwosb',edwosb
                        )
                    ) a1,duns_number dn, contract_number cn
    from contract_vehicle
    group by duns_number, contract_number)
select json_build_object(
        'contract_number'::text, contract_number::text,
        'info'::text, json_agg(
            json_build_object(
                    'category_id'::text, category::text,
                    'socio'::text, a1
                )
        )
    )
from contract_vehicle inner join cte on (duns_number=dn and contract_number = cn)
group by duns_number, contract_number;