Skip to content

Commit

Permalink
Add alias argument to `deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
judahrand committed Mar 22, 2022
1 parent 3a4cc94 commit 367e26b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For compatibility details between versions of dbt-core and dbt-utils, [see this

- [SQL generators](#sql-generators)
- [date_spine](#date_spine-source)
- [dedupe](#dedupe-source)
- [deduplicate](#deduplicate)
- [haversine_distance](#haversine_distance-source)
- [group_by](#group_by-source)
- [star](#star-source)
Expand Down Expand Up @@ -716,7 +716,8 @@ This macro returns the sql required to remove duplicate rows from a model or sou
{{ dbt_utils.deduplicate(
relation=source('my_source', 'my_table'),
group_by="user_id, cast(timestamp as day)",
order_by="timestamp desc"
order_by="timestamp desc",
alias="my_cte"
)
}}
```
Expand Down
10 changes: 5 additions & 5 deletions macros/sql/deduplicate.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{%- macro deduplicate(relation, group_by, order_by=none) -%}
{{ return(adapter.dispatch('deduplicate', 'dbt_utils')(relation, group_by, order_by=order_by)) }}
{%- macro deduplicate(relation, group_by, order_by=none, alias=none) -%}
{{ return(adapter.dispatch('deduplicate', 'dbt_utils')(relation, group_by, order_by=order_by, alias=alias)) }}
{% endmacro %}

{%- macro default__deduplicate(relation, group_by, order_by=none) -%}
{%- macro default__deduplicate(relation, group_by, order_by=none, alias=none) -%}

select
{{ dbt_utils.star(relation, relation_alias='deduped') | indent }}
Expand All @@ -15,7 +15,7 @@
order by {{ order_by }}
{%- endif %}
) as rn
from {{ relation }} as _inner
from {{ relation if alias is none else alias }} as _inner
) as deduped
where deduped.rn = 1

Expand All @@ -39,7 +39,7 @@
{%- endif %}
limit 1
)[offset(0)] as deduped
from {{ relation }} as original
from {{ relation if alias is none else alias }} as original
group by {{ group_by }}
)

Expand Down

0 comments on commit 367e26b

Please sign in to comment.