Skip to content

Instantly share code, notes, and snippets.

@ks--ks
Last active December 14, 2022 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ks--ks/9a58040176c6e42b25865326c02a03fa to your computer and use it in GitHub Desktop.
Save ks--ks/9a58040176c6e42b25865326c02a03fa to your computer and use it in GitHub Desktop.
Data sampling in PostgreSQL
-- sample data and assign new or returning user type
sampledata as (
select a.user_id,
a.created_at
, case
when a.created_at between a.firstdate and a.firstdate+27 then 'new'
else 'returning'
end as type
from (
select user_id
, left(right(user_id, 4), 2) as sample
, created_at
, firstdate::date as first_date
from data
group by 1, 2, 3, 4
) a
where a.sample='04'
group by 1, 2
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment