0

我这里有以下数据集

create table categories(
    id serial primary key,
    parent_id integer,
    name varchar(255) not null
);
create table entry(
    id serial primary key,
    location_id varchar(255) not null,
    location_string varchar(255) not null
);
create table entry_type(
    id serial primary key,
    name varchar(255) not null,
    category_ids integer[]
);
insert into categories(parent_id,name) values
(null,'a'), (null,'b'),(null,'c'), (1,'d'), (2,'e'), (5,'f');

insert into entry_type(name, category_ids) values
('entry_type_1', '{1}'),('entry_type_2', '{4, 3}'),('entry_type_3', '{5, 6}'),('entry_type_1', '{6}');

insert into entry(location_id, location_string) values
('1/4','a/d'),('2','b'),('5/6','e/f'),('5/6','e/f');

我正在运行这个查询:

select
    json_build_object(
            'id', u.id,
            'name', u.name,
            'parent_id', u.parent_id,
      'entry_type', json_agg(json_build_object(
                    'id', ur.id,
                    'name', ur.name
                )),
            'entries', json_agg(json_build_object(
                    'id', en.location_string
                ))
        )
from public.categories u
join public.entry_type ur on u.id = any(ur.category_ids)
join public.entry en on en.location_id like concat(u.id::text, '%')
group by u.id;

(这是所有这些的小提琴。)

但我想创建一个 Postgres 函数,该函数将返回结果(JSON),如下所述。

我设法接近预期的结果,但我没有想法。在我的查询中,我选择了所有类别,但我需要能够添加一个条件,例如:parent_id = "function_param"可能是 NULL,或者另一个 id/integer。

[
  {
    "id": "1",
    "name": "a",
    "parent_id": null,
    "entry_types": [
      {
        "id": "18"
      }
    ],
    "entries": [
      {
        "id": "54"
      },
      {
        "id": "22"
      }
    ],
    "entries_count": 2,
    "entry_types_count": 1
  },
  {
    "id": "2",
    "name": "b",
    "parent_id": null,
    "entry_types": [
      {
        "id": "88"
      }
    ],
    "entries": [
      {
        "id": "28"
      }
    ],
    "entries_count": 1,
    "entry_types_count": 1
  }
]
4

1 回答 1

0

只需将您的查询用json_agg

SELECT json_agg(j)
FROM (SELECT json_build_object(...) AS j
      FROM public.categories
      ...
     ) AS subq;
于 2020-06-16T11:32:11.137 回答