Skip to content

Instantly share code, notes, and snippets.

@vvgsrk
Created April 25, 2022 12:45
Show Gist options
  • Save vvgsrk/b8bfa047c62e821ce445d53ee5e94f44 to your computer and use it in GitHub Desktop.
Save vvgsrk/b8bfa047c62e821ce445d53ee5e94f44 to your computer and use it in GitHub Desktop.
Generate row rank using event timestamp
{%- 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