表:
TimeLimiter
http://sqlfiddle.com/#!17/04630/4
查询我尝试过:
Lazy
我希望按CREATE TABLE profit_center
(
id serial NOT NULL,
name character varying,
site_id integer,
CONSTRAINT profit_center_pkey PRIMARY KEY (id),
CONSTRAINT profit_center_name_site_id_key UNIQUE (name, site_id)
);
的所有select site_id,json_build_object(name,id) as jsn from profit_center;
值进行分组。单行预期结果:
jsn
答案 0 :(得分:0)
看起来很糟糕,但是returns your expected result:
with r as (select concat('"',site_id,'": ',concat('{',string_agg(concat('"',name,'": ',id),','),'}')::json) from profit_center group by site_id)
select concat('{',string_agg(concat,','),'}')::json from r;