Skip to content

Instantly share code, notes, and snippets.

@cdussud
Last active February 17, 2021 20:42
Show Gist options
  • Save cdussud/828b0f37c180b88a7bb55dbc62f259c2 to your computer and use it in GitHub Desktop.
Save cdussud/828b0f37c180b88a7bb55dbc62f259c2 to your computer and use it in GitHub Desktop.
Generates timestamps that increase following an exponential function
-- Generates timestamps that follow a function: y = r^x
with
increasing_timestamps as (
select
'2020-01-01'::timestamp as time_start,
sysdate as time_end,
date_diff('minute', time_start, time_end) as time_span_minutes,
random() as x, -- generate a random value from 0 to 1 for x
2^x - 1 as y, -- get our y, in this case also between 0 and 1 for simplicity
-- Note that when we're creating the timestamp for each row we're starting at time_end
-- and working backwords -- effectively flipping y on the x axis
-- We do this because we want the distribution of timestamps to look like our exponential function:
-- fewer sessions in the beginning and more towards the end
-- Think of y as the distance the timestamp is from the start time. Because our function is exponential,
-- bigger y values are futher and further apart from each other. This means the distance between timestamps
-- increases with bigger y. Flipping it gets us what we want: fewer timestamps at the start and more at the end
date_add('minute', -(time_span_minutes * y)::integer, time_end) as ts
from dw_demo.numbers n
where n.number < time_span_minutes / 2 -- one timestamp every two minutes
)
select
date_trunc('day', ts)::date AS day,
count(1) as total_timestamps
from increasing_timestamps
group by day
order by day asc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment