How to group by time bucket in ClickHouse and fill missing data with nulls/0s

前端 未结 4 1248
迷失自我
迷失自我 2021-01-12 04:15

Suppose I have a given time range. For explanation, let\'s consider something simple, like whole year 2018. I want to query data from ClickHouse as a sum aggregation for eac

4条回答
  •  醉梦人生
    2021-01-12 04:26

    As an alternative for numbers() function in some cases range and array functions can be useful.

    Example: for each pair of (id1,id2) dates from the previous 7 days should be generated.

    SELECT
      id1,
      id2,
      arrayJoin(
        arrayMap( x -> today() - 7 + x, range(7) )
      ) as date2
    FROM table
    WHERE date >= now() - 7
    GROUP BY id1, id2
    

    The result of that select can be used in UNION ALL to fill the 'holes' in data.

    SELECT id1, id2, date, sum(column1)
    FROM (
      SELECT
        id1,
        id2,
        date,
        column1 
      FROM table
      WHERE date >= now() - 7
    
      UNION ALL 
    
      SELECT
        id1,
        id2,
        arrayJoin(
          arrayMap( x -> today() - 7 + x, range(7) )
        ) as date2,
        0 as column1
      FROM table
      WHERE date >= now() - 7
      GROUP BY id1, id2
    )
    GROUP BY id1, id2, date
    ORDER BY date, id1, id2
    

提交回复
热议问题