Created
April 25, 2022 12:45
-
-
Save vvgsrk/b8bfa047c62e821ce445d53ee5e94f44 to your computer and use it in GitHub Desktop.
Generate row rank using event timestamp
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
{%- macro generate_row_rank_using_event_ts(model, keycolumn) -%} | |
{%- set columns = adapter.get_columns_in_relation(model) -%} | |
WITH latest_data_changes AS | |
( | |
SELECT | |
ROW_NUMBER() OVER ( | |
PARTITION BY {{ keycolumn }} | |
ORDER BY __event_ts DESC | |
) AS row_rank, | |
* | |
FROM {{ model }} | |
) | |
SELECT | |
{% for column in columns %} | |
"{{ column.name }}"{% if not loop.last -%}, | |
{% endif -%} | |
{%- endfor %} | |
FROM latest_data_changes | |
WHERE row_rank = 1 | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment