WITH FILL clause can come in handy.
This guide discusses how to use WITH FILL to fill gaps in your time-series data.
Setup
Imagine we’ve got the following table that stores metadata on images generated by a GenAI image service:Querying by bucket
We’re going to explore the images created between00:24:03 and 00:24:04 on the 24th March 2023, so let’s create some parameters for those points in time:
WITH FILL
We can use theWITH FILL clause to fill in these gaps.
We’ll also specify the STEP, which is the size of the gaps to fill.
This defaults to 1 second for DateTime types, but we’d like to fill gaps of 100ms in length, so let’s an interval of 100ms as our step value:
count column.
WITH FILL…FROM
There is, however, still a gap at the beginning of the time range, which we can fix by specifyingFROM:
00:24:03.000 to 00:24:03.500 all now appear.
WITH FILL…TO
We’re still missing some buckets from the end of the time range though, which we can fill by providing aTO value.
TO isn’t inclusive, so we’ll add a small amount to the end time to make sure that it’s included:
00:24:03.000 to 00:24:05.000.
Cumulative count
Let’s say we now want to keep a cumulative count of the number of images created across the buckets. We can do this by adding acumulative column, as shown below:
WITH FILL…INTERPOLATE
Any rows that have0 in the count column also have 0 in the cumulative column, whereas we’d rather it use the previous value in the cumulative column.
We can do this by using the INTERPOLATE clause, as shown below:
bar function, not forgetting to add our new column to the INTERPOLATE clause.