sql - 递归查询中的排序结果

标签 sql json postgresql recursive-query jsonb

我有一个类似CATEGORIES的基本表,由primary_keyparent_idtitle等字段组成和一个排序整数。

我能够使用 CTE 检索结果并将它们转换为 json 数组,但我想根据排序值获取它们,除了 parent_id。

到目前为止:

with recursive parents as
(
    select n.boat_type_id, n.title, '{}'::int[] as parents, 0 as level
    from boat_types n
    where n.parent_id is NULL
    union all
    select n.boat_type_id, n.title, parents || n.parent_id, level+1
    from parents p
        join boat_types n on n.parent_id = p.boat_type_id
    where not n.boat_type_id = any(parents)
),
children as
(
    select n.parent_id, json_agg(jsonb_build_object('title', n.title->>'en'))::jsonb as js
    from parents tree
        join boat_types n using(boat_type_id)
    where level > 0 and not boat_type_id = any(parents)
    group by n.parent_id
    union all
    select n.parent_id, jsonb_build_object('category', n.title->>'en') || jsonb_build_object('subcategories', js) as js
    from children tree
        join boat_types n on n.boat_type_id = tree.parent_id
)
select jsonb_agg(js) as categories
from children
where parent_id is null  

上面为我提供了我想要的结果集和结构,但是我怎样才能让它们遵循节点和叶子的排序值。

示例响应:

[
   {
      "sorting":0,
      "category":"Motor",
      "subcategories":[
         {
            "title":"Motor Yacht",
            "sorting":2
         },
         {
            "title":"Mega Yacht",
            "sorting":1
         }
      ]
   },
   {
      "sorting":1,
      "category":"Sailing",
      "subcategories":[
         {
            "title":"Sailing Yacht",
            "sorting":2
         },
         {
            "title":"Cruiser Racer",
            "sorting":1
         }
      ]
   },
   {
      "sorting":2,
      "category":"Catamaran",
      "subcategories":[
         {
            "title":"Catamaran",
            "sorting":2
         },
         {
            "title":"Trimaran",
            "sorting":1
         }
      ]
   },
   {
      "sorting":3,
      "category":"Other",
      "subcategories":[
         {
            "title":"Other",
            "sorting":2
         },
         {
            "title":"Airboat",
            "sorting":1
         }
      ]
   }
]

我试过聚合 ARRAY 字段中的排序值并按它排序,但它不起作用。

最佳答案

您可以在 json_agg() 聚合中使用 order by 子句:

...
children as
(
    select 
        n.parent_id, 
        json_agg(jsonb_build_object('title', n.title->>'en', 'sorting', n.sorting) order by n.sorting)::jsonb as js
    from parents tree
        join boat_types n using(boat_type_id)
    where level > 0 and not boat_type_id = any(parents)
    group by n.parent_id
    union all
    select 
        n.parent_id, 
        jsonb_build_object('category', n.title->>'en', 'sorting', n.sorting) || jsonb_build_object('subcategories', js) as js
    from children tree
        join boat_types n on n.boat_type_id = tree.parent_id
)
...

关于sql - 递归查询中的排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348500/

相关文章:

mysql - sql多表查询

来自查询的 SQL 数据透视表

javascript - 如何使用 Reactjs 显示 xml 数据

javascript - 将变量传递给 Json API 并使用 ng-repeat 显示响应

sql - SQL 如何获取一组项目的平均值?

php - 确认完成后将详细信息从一张表移至另一张表 [sql]

sql - 选择列、计数(列),但按第三列值排序

javascript - 使用最新 Ember Data 版本中的 RESTSerializer 格式化 JSON

sql - 使用 Node.js 客户端和连接池在 Postgres 中最快的查询 - 存储函数或准备好的语句?

mysql - Octopus gem 不适用于事件记录关系