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
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