【发布时间】:2018-01-16 07:55:46
【问题描述】:
我的查询以下列方式显示结果:
{"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;
我正在尝试重构该查询以返回如下所示的 json:
{"2018-01-16": {"positive": 2, "declined": "1", "negative": 1}}
我该怎么做?这是您可以尝试的 sql fiddle:
http://sqlfiddle.com/#!17/a9705/8
提前致谢。
【问题讨论】:
标签: sql postgresql postgresql-9.4 jsonb