text: SELECT day
  -- 1) Subquery to generate all dates
  -- from min to max date_created
    FROM (SELECT GENERATE_SERIES(MIN(date_created),
                                 MAX(date_created),
                                 '1 day')::date AS day
            -- What table is date_created in?
            FROM evanston311) AS all_dates
  -- 4) Select dates (day from above) that are NOT IN the subquery
   WHERE day NOT IN 
         -- 2) Subquery to select all date_created values as dates
         (SELECT date_created::date
            FROM evanston311);
  
  
  -- Generate 6 month bins covering 2016-01-01 to 2018-06-30
  
  -- Create lower bounds of bins
  SELECT generate_series('2016-01-01',  -- First bin lower value
                         '2018-01-01',  -- Last bin lower value
                         '6 months'::interval) AS lower,
  -- Create upper bounds of bins
         generate_series('2016-07-01',  -- First bin upper value
                         '2018-07-01',  -- Last bin upper value
                         '6 months'::interval) AS upper;
  
  
  -- Count number of requests made per day
  SELECT day, COUNT(date_created) AS count
  -- Use a daily series from 2016-01-01 to 2018-06-30 
  -- to include days with no requests
    FROM (SELECT generate_series('2016-01-01',  -- series start date
                                 '2018-06-30',  -- series end date
                                 '1 day'::interval)::date AS day) AS daily_series
         LEFT JOIN evanston311
         -- match day from above (which is a date) to date_created
         ON day = date_created::date
   GROUP BY day;
  
  
  -- Bins from Step 1
  WITH bins AS (
  	 SELECT generate_series('2016-01-01',
                              '2018-01-01',
                              '6 months'::interval) AS lower,
              generate_series('2016-07-01',
                              '2018-07-01',
                              '6 months'::interval) AS upper),
  -- Daily counts from Step 2
       daily_counts AS (
       SELECT day, count(date_created) AS count
         FROM (SELECT generate_series('2016-01-01',
                                      '2018-06-30',
                                      '1 day'::interval)::date AS day) AS daily_series
              LEFT JOIN evanston311
              ON day = date_created::date
        GROUP BY day)
  -- Select bin bounds 
  SELECT lower, 
         upper, 
         -- Compute median of count for each bin
         PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY day) AS median
    -- Join bins and daily_counts
    FROM bins
         LEFT JOIN daily_counts
         -- Where the day is between the bin bounds
         ON day >= lower
            AND day < upper
   -- Group by bin bounds
   GROUP BY lower, upper
   ORDER BY lower;
  
  
  -- generate series with all days from 2016-01-01 to 2018-06-30
  WITH all_days AS 
       (SELECT GENERATE_SERIES('2016-01-01',
                               '2018-06-30',
                               '1 day'::interval) AS date),
       -- Subquery to compute daily counts
       daily_count AS 
       (SELECT DATE_TRUNC('day', date_created) AS day,
               COUNT(*) AS count
          FROM evanston311
         GROUP BY day)
  -- Aggregate daily counts by month using date_trunc
  SELECT DATE_TRUNC('month', date) AS month,
         -- Use coalesce to replace NULL count values with 0
         AVG(COALESCE(COUNT,0)) AS average
    FROM all_days
         LEFT JOIN daily_count
         -- Joining condition
         ON all_days.date=daily_count.day
   GROUP BY month
   ORDER BY month; 
  		    
  