Skip to content

Instantly share code, notes, and snippets.

@cdussud
Last active February 15, 2021 21:48
Show Gist options
  • Save cdussud/b3a9c6929dc122a07d1d3a36de8b7069 to your computer and use it in GitHub Desktop.
Save cdussud/b3a9c6929dc122a07d1d3a36de8b7069 to your computer and use it in GitHub Desktop.
Random timestamps between two intervals in Redshift
-- generates timestamps randomly between the start and end times
with
random_timestamps as (
select
-- some 'variables' just to make things clear
'2021-01-01'::timestamp as time_start,
sysdate as time_end,
date_diff('minute', time_start, time_end) as time_span_minutes,
-- pick a random number (0..1) and multiply by total number of minutes
-- offset that from the start time to get a random time stamp in the chosen interval
date_add('minute',(time_span_minutes * random())::integer, time_start) as ts
from dw_demo.numbers n
where n.number < time_span_minutes / 2 -- one timestamp every two minutes
)
select
date_trunc('day', ts) AS day,
count(1) as total_timestamps
from random_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