Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL高级记录计数_Sql_Postgresql_Postgresql 9.4 - Fatal编程技术网

PostgreSQL高级记录计数

PostgreSQL高级记录计数,sql,postgresql,postgresql-9.4,Sql,Postgresql,Postgresql 9.4,我有以下模式: CREATE TABLE survey_results ( id integer NOT NULL, scores jsonb DEFAULT '{}'::jsonb, created_at timestamp without time zone, updated_at timestamp without time zone ); INSERT INTO survey_results (id, scores, created_at, upda

我有以下模式:

CREATE TABLE survey_results (
    id integer NOT NULL,
    scores jsonb DEFAULT '{}'::jsonb,
    created_at timestamp without time zone,
    updated_at timestamp without time zone  
);

INSERT INTO survey_results (id, scores, created_at, updated_at)
    VALUES (1, '{"medic": { "categories": { "motivation": "high" } } }', '2017-10-01', '2017-10-01');

INSERT INTO survey_results (id, scores, created_at, updated_at)
    VALUES (2, '{"medic": { "categories": { "motivation": "medium" } } }', '2017-10-02', '2017-10-02');

INSERT INTO survey_results (id, scores, created_at, updated_at)
    VALUES (3, '{"medic": { "categories": { "motivation": "low" } } }', '2017-10-03', '2017-10-03');
我通过以下查询从此表中获取数据:

SELECT
  date(survey_results.created_at),
  json_build_object(
    'high', COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('high'))),
    'medium', COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('medium'))),
    'low', COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('low')))
  ) as motivation                               
  FROM survey_results
  GROUP BY date(survey_results.created_at)
  ORDER BY date asc;
以以下格式返回数据:

   date    |              motivation
------------------------------------------------------
2017-10-01 |    {"high" : 1, "medium" : 0, "low" : 0}
2017-10-02 |    {"high" : 0, "medium" : 1, "low" : 0}
2017-10-03 |    {"high" : 0, "medium" : 0, "low" : 1}
例如,如果日期为2017-10-01,则应包括从时间开始到2017-10-01结束的所有调查结果。第二个日期将包括第一个日期的所有调查结果加上2017-10-02年收集的调查结果,依此类推

因此,结果应该是:

   date    |              motivation
------------------------------------------------------
2017-10-01 |    {"high" : 1, "medium" : 0, "low" : 0}
2017-10-02 |    {"high" : 1, "medium" : 1, "low" : 0}
2017-10-03 |    {"high" : 1, "medium" : 1, "low" : 1}
以下是sql FIDLE与模式和基本查询:

在PostgreSQL中有没有这样做的方法?

我想您需要:

SELECT date(survey_results.created_at),
      json_build_object(
    'high', SUM(COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('high')))) OVER (ORDER BY date(survey_results.created_at)),
    'medium', SUM(COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('medium')))) OVER (ORDER BY date(survey_results.created_at)),
    'low', SUM(COUNT(*) FILTER (WHERE (scores#>>'{medic,categories,motivation}' in('low')))) OVER (ORDER BY date(survey_results.created_at))
  ) as motivation                               
FROM survey_results
GROUP BY date(survey_results.created_at)
ORDER BY date asc;
也就是说,使用累积总和


作为一句忠告,如果没有所有的JSON对象内容,您的查询将更容易处理和理解。你可能想让查询工作,然后在所有的工作之后添加JSON格式。< /P>所以每天都要考虑前一天或之前的所有日子。没错,你是对的。如果属性总是相同的,你可以为每一行提取它们,并使用<代码>和(…)(在无界的前一行和当前行之间按日期排序)。窗口函数是你的朋友。@LaurenzAlbe-Hmm你能举个例子吗?我做了,但我不会为你写代码。请看JSON函数的文档。可能需要一个子查询。谢谢你的回复,不幸的是,这个查询不起作用,它返回:
错误:语法错误在或接近“结束”位置:160
@MateuszUrbański…我想括号放错地方了。