I have a table in Clickhouse with the structure:
x_id | y_id | z_id | rank | timestamp
1231 | 1324 | 9412 | 1 | 2021-03-12 00:13:34
121 | 5524 | 765 | 21 | 2021-03-13 15:43:21
54 | 76 | 8822 | 125 | 2021-05-14 17:23:12
213 | 61 | 7651 | 51 | 2021-03-16 12:15:43
53 | 65 | 123 | 23 | 2021-03-12 13:28:54
1231 | 432 | 7651 | 1541 | 2021-03-12 16:54:24
...
There are no records for particular groups (x_id, y_id, z_id) for some weeks and in this case I need to take the previous rank (from the previous week) for this group (x_id, y_id, z_id) if a value exists.
For example:
group_ids, rank, timestamp
(1, 1, 1, 25, '2021-03-12 00:13:34') -> group (1, 1, 1), week 2021-03-08
(2, 2, 2, 30, '2021-03-16 00:13:34') -> group (2, 2, 2), week 2021-03-15
no data for group (1, 1, 1) for week 2021-03-15 - fill from the previous week and set "week" as the current week:
(1, 1, 1, 25, 2021-03-15)
and so on ...
and than count metrics for this data using subquery
SELECT
week,
SUM(CASE
WHEN rank BETWEEN 1 AND 3 THEN 1
ELSE 0
END) AS metric1,
/* ... */
FROM (
SELECT min(rank) AS rank, toStartOfWeek(Timestamp, 1) AS week FROM table GROUP BY week, x_id, y_id, z_id
) GROUP BY week ORDER BY week;
metric1 | metric2 | week
0 | 2 | 2021-03-22
1 | 0 | 2021-03-29
0 | 1 | 2021-04-05
Is it possible to build a query with forward filling missing values?