Sql 使用“按日期分组”时缺少日期

Sql 使用“按日期分组”时缺少日期,sql,postgresql,Sql,Postgresql,在我的PostgreSQL数据库中,我有调查结果表: 我在本表中有以下记录: INSERT INTO survey_results (id, scores, created_at, updated_at) VALUES (1, '{"medic": { "social": { "total": "high" } } }', '2018-01-11', '2018-01-10'); INSERT INTO survey_results (id, scores, created_at, u

在我的PostgreSQL数据库中,我有调查结果表:

我在本表中有以下记录:

INSERT INTO survey_results (id, scores, created_at, updated_at)
    VALUES (1, '{"medic": { "social": { "total": "high" } } }', '2018-01-11', '2018-01-10');

INSERT INTO survey_results (id, scores, created_at, updated_at)
    VALUES (2, '{"medic": { "social": { "total": "high" } } }', '2018-01-12', '2018-01-12');
以及以下查询:

SELECT
  distinct(date(survey_results.created_at)),

  ROUND(
    COUNT(*) FILTER (WHERE (
      scores#>>'{medic,social,total}' in('high'))) OVER(order by date(survey_results.created_at)
    ) * 1.0 /

    (
      GREATEST(
        COUNT(*) FILTER (WHERE (scores#>>'{medic,social,total}' in('high','medium','low')
    )
  ) OVER(order by date(survey_results.created_at)), 1.0))* 100, 2
)
 AS positive

  FROM survey_results
  WHERE  
    survey_results.created_at::date >= '2018-01-10'
    AND survey_results.created_at::date <= '2018-01-12'
  GROUP BY date, scores
  ORDER BY date ASC;
问题是查询遗漏了2018-01-10,因为没有记录,这是因为group by。是否有任何方法更新此查询以同时返回没有记录的天数:

date        positive
2018-01-10  0
2018-01-11  100
2018-01-12  100
这是sqlfiddle:


日期没有遗漏。可以这样想:您的查询要求服务器返回所有创建的值为>='2018-01-10'且创建的值为的数据。请使用该函数

date        positive
2018-01-11  100
2018-01-12  100
date        positive
2018-01-10  0
2018-01-11  100
2018-01-12  100
DECLARE @dtStartDate datetime
DECLARE @dtEndDate datetime
SET @dtStartDate = '2018-01-10'
SET @dtEndDate = '2018-01-12'

SELECT
  T.DateVal,
  CASE DATEPART(weekday, T.DateVal)
    WHEN 1 THEN 'Sunday'
    WHEN 2 THEN 'Monday'
    WHEN 3 THEN 'Tuesday'
    WHEN 4 THEN 'Wednesday'
    WHEN 5 THEN 'Thursday'
    WHEN 6 THEN 'Friday'
    WHEN 7 THEN 'Saturday'
  END AS WeekDay,
  DATEPART(day, T.DateVal) AS [Date],
  DATEPART(month, T.DateVal) AS [Month],
  DATEPART(year, T.DateVal) AS [Year]
FROM
(
  SELECT
      DATEADD(day, SEQ.SeqValue, @dtStartDate) DateVal
  FROM
  (
  SELECT
      (HUNDREDS.SeqValue + TENS.SeqValue + ONES.SeqValue) SeqValue
  FROM
      (
      SELECT 0  SeqValue
      UNION ALL
      SELECT 1 SeqValue
      UNION ALL
      SELECT 2 SeqValue
      UNION ALL
      SELECT 3 SeqValue
      UNION ALL
      SELECT 4 SeqValue
      UNION ALL
      SELECT 5 SeqValue
      UNION ALL
      SELECT 6 SeqValue
      UNION ALL
      SELECT 7 SeqValue
      UNION ALL
      SELECT 8 SeqValue
      UNION ALL
      SELECT 9 SeqValue
      ) ONES
  CROSS JOIN
      (
      SELECT 0 SeqValue
      UNION ALL
      SELECT 10 SeqValue
      UNION ALL
      SELECT 20 SeqValue
      UNION ALL
      SELECT 30 SeqValue
      UNION ALL
      SELECT 40 SeqValue
      UNION ALL
      SELECT 50 SeqValue
      UNION ALL
      SELECT 60 SeqValue
      UNION ALL
      SELECT 70 SeqValue
      UNION ALL
      SELECT 80 SeqValue
      UNION ALL
      SELECT 90 SeqValue
      ) TENS
  CROSS JOIN
      (
      SELECT 0 SeqValue
      UNION ALL
      SELECT 100 SeqValue
      UNION ALL
      SELECT 200 SeqValue
      UNION ALL
      SELECT 300 SeqValue
      UNION ALL
      SELECT 400 SeqValue
      UNION ALL
      SELECT 500 SeqValue
      UNION ALL
      SELECT 600 SeqValue
      UNION ALL
      SELECT 700 SeqValue
      UNION ALL
      SELECT 800 SeqValue
      UNION ALL
      SELECT 900 SeqValue
      ) HUNDREDS
  ) SEQ
) T
WHERE
  T.DateVal <= @dtEndDate
ORDER BY
  T.DateVal ASC
SELECT date::date, coalesce(positive, 0.00) as positive
FROM generate_series('2018-01-10'::date, '2018-01-12', '1d') s(date)
LEFT JOIN (
    -- your query
    SELECT
      distinct(date(survey_results.created_at)),
      ROUND(
        COUNT(*) FILTER (WHERE (
          scores#>>'{medic,social,total}' in('high'))) OVER(order by date(survey_results.created_at)
        ) * 1.0 /
        (
          GREATEST(
            COUNT(*) FILTER (WHERE (scores#>>'{medic,social,total}' in('high','medium','low')
        )
      ) OVER(order by date(survey_results.created_at)), 1.0))* 100, 2
    )
     AS positive
      FROM survey_results
      WHERE  
        survey_results.created_at::date >= '2018-01-10'
        AND survey_results.created_at::date <= '2018-01-12'
      GROUP BY date, scores
    -- your query
    ) q USING(date)
ORDER BY date ASC;

    date    | positive 
------------+----------
 2018-01-10 |     0.00
 2018-01-11 |   100.00
 2018-01-12 |   100.00
(3 rows)