Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created September 11, 2017 22:35
Show Gist options
  • Save ryantuck/706e8cc7397a58c7b399dfc2cd0040b8 to your computer and use it in GitHub Desktop.
Save ryantuck/706e8cc7397a58c7b399dfc2cd0040b8 to your computer and use it in GitHub Desktop.
messing around with sequences in postgres
drop table if exists seq_test;
create table seq_test as
with
dates as (
select
0 as id,
generate_series(
'2017-01-01'::date,
'2017-01-07'::date,
'1 day'::interval)::date as date
),
sequences as (
select
*,
row_number() over w as sequence
from
dates
window w as (
partition by id
order by date
)
),
vals as (
select
*,
first_value(sequence) over w as first_seq,
last_value(sequence) over w as last_seq
from
sequences
window w as (
partition by id
order by date
rows between unbounded preceding and unbounded following
)
)
select
*,
case
when sequence = last_seq then true
else false
end as is_latest_sequence
from
vals
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment