Missing dates when using GROUP BY date

Mateusz Urbański

In my PostgreSQL database I have survey_results table:

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

I have the following records in this table:

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');

and following 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
  ORDER BY date ASC;

which returns:

date        positive
2018-01-11  100
2018-01-12  100

The problem is that query omitted 2018-01-10 because there was not records, that's because of group by. Is there any way to update this query to also return days that do not have records:

date        positive
2018-01-10  0
2018-01-11  100
2018-01-12  100

Here is sqlfiddle:

http://sqlfiddle.com/#!17/5e007/1

klin

Use the function generate_series():

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)    

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Incrementally increasing date using lag function for missing dates only

分類Dev

Group Events by Date using KnockoutJS

分類Dev

Find several maximum dates in same group using pyspark

分類Dev

Excel PivotTable Sorts Dates Wrong When Grouping by Date

分類Dev

MongoDB aggregation to add missing months between two dates after grouping on date field

分類Dev

How to count cases by group with n() when there are missing data

分類Dev

Data/Contents are missing when using Python Selenium

分類Dev

What am I missing when using PROGMEM?

分類Dev

Group dates in a foreach loop

分類Dev

Populate list with missing dates LINQ

分類Dev

How to fill up missing dates?

分類Dev

Combining Dates and Ordering by Date

分類Dev

'not a single-group group function' in oracle when using LISTAGG

分類Dev

How to group dates into years, when the year starts on a month other than January

分類Dev

want to get all dates but when there is one or more TDBUY = 1 then this date@TDBUY=0 not

分類Dev

Missing Leaflet Map Tiles when using react-leaflet

分類Dev

how to get the missing values in SQL query when using in clause

分類Dev

ORA:00907: missing right parenthesis, when using CASE in WHERE clause

分類Dev

Group by summarize in between dates with dplyr

分類Dev

How to group dates and associated state

分類Dev

Getting 'Missing required field: member' when trying to add a member to a google group via API

分類Dev

How to find difference between two dates using date_diff() from array

分類Dev

Using Spark 2.3.1 with Scala, Reduce Arbitrary List of Date Ranges into distinct non-overlapping ranges of dates

分類Dev

Return specific dates, months from any year using python date time

分類Dev

Count total missing values by group?

分類Dev

Count total missing values by group?

分類Dev

Group by and fill missing datetime values

分類Dev

In proc sql when using SELECT * and GROUP BY, the result is not collapsed

分類Dev

Error when including a contact to group using People API

Related 関連記事

  1. 1

    Incrementally increasing date using lag function for missing dates only

  2. 2

    Group Events by Date using KnockoutJS

  3. 3

    Find several maximum dates in same group using pyspark

  4. 4

    Excel PivotTable Sorts Dates Wrong When Grouping by Date

  5. 5

    MongoDB aggregation to add missing months between two dates after grouping on date field

  6. 6

    How to count cases by group with n() when there are missing data

  7. 7

    Data/Contents are missing when using Python Selenium

  8. 8

    What am I missing when using PROGMEM?

  9. 9

    Group dates in a foreach loop

  10. 10

    Populate list with missing dates LINQ

  11. 11

    How to fill up missing dates?

  12. 12

    Combining Dates and Ordering by Date

  13. 13

    'not a single-group group function' in oracle when using LISTAGG

  14. 14

    How to group dates into years, when the year starts on a month other than January

  15. 15

    want to get all dates but when there is one or more TDBUY = 1 then this date@TDBUY=0 not

  16. 16

    Missing Leaflet Map Tiles when using react-leaflet

  17. 17

    how to get the missing values in SQL query when using in clause

  18. 18

    ORA:00907: missing right parenthesis, when using CASE in WHERE clause

  19. 19

    Group by summarize in between dates with dplyr

  20. 20

    How to group dates and associated state

  21. 21

    Getting 'Missing required field: member' when trying to add a member to a google group via API

  22. 22

    How to find difference between two dates using date_diff() from array

  23. 23

    Using Spark 2.3.1 with Scala, Reduce Arbitrary List of Date Ranges into distinct non-overlapping ranges of dates

  24. 24

    Return specific dates, months from any year using python date time

  25. 25

    Count total missing values by group?

  26. 26

    Count total missing values by group?

  27. 27

    Group by and fill missing datetime values

  28. 28

    In proc sql when using SELECT * and GROUP BY, the result is not collapsed

  29. 29

    Error when including a contact to group using People API

ホットタグ

アーカイブ