以不同格式呈现查询数据 - PostgreSQL

Present query data in different format - PostgreSQL

我有以下方式显示结果的查询:

{"declined": [{"2018-01-16": 1}], "negative": [{"2018-01-16": 1}], "positive": [{"2018-01-16": 2}]}

查询如下所示:

select 
jsonb_build_object('positive', 
  json_agg(jsonb_build_object(date, positive)) FILTER (WHERE positive <> 0)
) ||
jsonb_build_object('negative',
  json_agg( jsonb_build_object(date, negative)) FILTER (WHERE negative <> 0)
) ||
jsonb_build_object('declined', 
  json_agg(jsonb_build_object(date, declined)) FILTER (WHERE declined <> 0)
)
from (
SELECT
  date(survey_results.created_at) as date,
  COUNT(*) FILTER (WHERE (scores#>>'{medic,social,total}' in('high','medium'))) AS positive,
  COUNT(*) FILTER (WHERE (scores#>>'{medic,social,total}' in('low'))) AS negative,
  COUNT(*) FILTER (WHERE (coalesce(raw#>>'{survey,denied}','f') = 'true')) AS declined
  FROM survey_results
  GROUP BY date(survey_results.created_at)
) as t1;

我正在尝试将该查询重构为 return 我 json 看起来像这样:

{"2018-01-16": {"positive": 2, "declined": "1", "negative": 1}}

我该怎么做?这是您可以试验的 sql fiddle:

http://sqlfiddle.com/#!17/a9705/8

提前致谢。

我可能遗漏了 smth - 我只是 jsonb_build_objectjsonb_build_object:

select 
jsonb_build_object(date,jsonb_build_object('positive',positive,'negative',negative,'declined',declined))
from (
SELECT
  date(survey_results.created_at) as date,
  COUNT(*) FILTER (WHERE (scores#>>'{medic,social,total}' in('high','medium'))) AS positive,
  COUNT(*) FILTER (WHERE (scores#>>'{medic,social,total}' in('low'))) AS negative,
  COUNT(*) FILTER (WHERE (coalesce(raw#>>'{survey,denied}','f') = 'true')) AS declined
  FROM survey_results
  GROUP BY date(survey_results.created_at)
) t1

结果:

{"2018-01-16": {"declined": 1, "negative": 1, "positive": 2}}