将分组数据从表中获取为树结构的 JSON 对象的最佳方法是什么

问题描述 投票:0回答:1

我需要将 ORACLE 表中的分组数据作为树结构获取到 JSON 对象中

Oracle 版本 19c(如果重要的话)

桌子会是这样的:

create table a_table (
    store    varchar2(100),
    brand    varchar2(100),
    product  varchar2(100),
    quantity number,
    amount   number
);

insert into a_table t (store, brand, product, quantity, amount) values ('All Motors Store', 'BWM', 'Car', 22, 57000);
insert into a_table t (store, brand, product, quantity, amount) values ('All Motors Store', 'BWM', 'Motorbike', 66, 37000);
insert into a_table t (store, brand, product, quantity, amount) values ('All Motors Store', 'CGM', 'Car', 88, 61000);
insert into a_table t (store, brand, product, quantity, amount) values ('All Motors Store', 'CGM', 'Motorbike', 77, 25000);
insert into a_table t (store, brand, product, quantity, amount) values ('All Motors Store', 'CGM', 'Bicycle', 14, 2000);
insert into a_table t (store, brand, product, quantity, amount) values ('Vehicle Store', 'BWM', 'Car', 2, 40000);
insert into a_table t (store, brand, product, quantity, amount) values ('Vehicle Store', 'BWM', 'Motorbike', 6, 22000);
insert into a_table t (store, brand, product, quantity, amount) values ('Vehicle Store', 'BWM', 'Bicycle', 6, 2300);
insert into a_table t (store, brand, product, quantity, amount) values ('Vehicle Store', 'CGM', 'Car', 8, 50000);
insert into a_table t (store, brand, product, quantity, amount) values ('Vehicle Store', 'CGM', 'Motorbike', 7, 21000);
commit;

结果结构一定是这样的:

{
    "items": [
        {
            "key": "All Motors Store",
            "summary": [267, 182000],
            "items": [
                {
                    "key": "BWM",
                    "summary": [88, 94000],
                    "items": [
                        {
                            "key": "Car",
                            "summary": [22, 57000]
                        },
                        {
                            "key": "Motorbike",
                            "summary": [66, 37000]
                        }
                    ]
                },
                {
                    "key": "CGM",
                    "summary": [179, 88000],
                    "items": [
                        {
                            "key": "Bicycle",
                            "summary": [14, 2000]
                        },
                        {
                            "key": "Car",
                            "summary": [88, 61000]
                        },
                        {
                            "key": "Motorbike",
                            "summary": [77, 25000]
                        }
                    ]
                }
            ]
        },
        {
            "key": "Vehicle Store",
            "summary": [29, 135300],
            "items": [
                {
                    "key": "BWM",
                    "summary": [14, 64300],
                    "items": [
                        {
                            "key": "Bicycle",
                            "summary": [6, 2300]
                        },
                        {
                            "key": "Car",
                            "summary": [2, 40000]
                        },
                        {
                            "key": "Motorbike",
                            "summary": [6, 22000]
                        }
                    ]
                },
                {
                    "key": "CGM",
                    "summary": [15, 71000],
                    "items": [
                        {
                            "key": "Car",
                            "summary": [8, 50000]
                        },
                        {
                            "key": "Motorbike",
                            "summary": [7, 21000]
                        }
                    ]
                }
            ]
        }
    ],
    "summary": [296, 317300]
}

最好的方法是什么?

sql oracle plsql oracle19c
1个回答
0
投票

GROUP BY store, brand
并聚合,然后使用第二次传递到
GROUP BY store
并再次聚合,然后使用第三次传递对整个结果集进行聚合:

SELECT JSON_OBJECT(
         KEY 'items' VALUE JSON_ARRAYAGG(
           JSON_OBJECT(
             KEY 'key' VALUE store,
             KEY 'summary' VALUE JSON_ARRAY(SUM(brand_quantity), SUM(brand_amount)),
             KEY 'items' VALUE JSON_ARRAYAGG(json FORMAT JSON)
           )
           RETURNING CLOB
         ),
         KEY 'summary' VALUE JSON_ARRAY(
           SUM(SUM(brand_quantity)),
           SUM(SUM(brand_amount))
         )
         RETURNING CLOB
       ) AS json
FROM   (
  SELECT store,
         SUM(quantity) AS brand_quantity,
         SUM(amount) AS brand_amount,
         JSON_OBJECT(
           KEY 'key' VALUE brand,
           KEY 'summary' VALUE JSON_ARRAY(SUM(quantity), SUM(amount)),
           KEY 'items' VALUE JSON_ARRAYAGG(
             JSON_OBJECT(
               KEY 'key' VALUE product,
               KEY 'summary' VALUE JSON_ARRAY(quantity, amount)
               RETURNING CLOB
             )
             RETURNING CLOB
           )
           RETURNING CLOB
         ) AS json
  FROM   a_table
  GROUP BY store, brand
)
GROUP BY store

对于样本数据,输出:

JSON
{"items":[{"key":"所有汽车商店","summary":[267,182000],"items":[{"key":"BWM","summary":[88,94000 ],"items":[{"key":"汽车","summary":[22,57000]},{"key":"摩托车","summary":[66,37000]}]},{ "key":"CGM","summary":[179,88000],"items":[{"key":"汽车","summary":[88,61000]},{"key":"自行车","summary":[14,2000]},{"key":"摩托车","summary":[77,25000]}]}]},{"key":"车辆商店","summary" :[29,135300],"项目":[{"密钥":"BWM","摘要":[14,64300],"项目":[{"密钥":"汽车","摘要":[ 2,40000]},{"key":"自行车","summary":[6,2300]},{"key":"摩托车","summary":[6,22000]}]},{" key":"CGM","summary":[15,71000],"items":[{"key":"汽车","summary":[8,50000]},{"key":"摩托车" ,“摘要”:[7,21000]}]}]}],“摘要”:[296,317300]}

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.