Last active
October 25, 2024 04:14
-
-
Save ks--ks/9a58040176c6e42b25865326c02a03fa to your computer and use it in GitHub Desktop.
Data sampling in PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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