Skip to content

Commit

Permalink
Fix week comparison
Browse files Browse the repository at this point in the history
first_date and second_date need to be compared in sql instead of jinja, as their values are going to come from the sql data.
  • Loading branch information
mjumbewu committed Feb 24, 2020
1 parent 2b1f3bc commit 7871a21
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions macros/cross_db_utils/datediff.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
({{ dbt_utils.datediff(first_date, second_date, 'year') }} * 12 + date_part('month', ({{second_date}})::date) - date_part('month', ({{first_date}})::date))
{% elif datepart == 'day' %}
(({{second_date}})::date - ({{first_date}})::date)
{% elif datepart == 'week' and first_date <= second_date %}
({{ dbt_utils.datediff(first_date, second_date, 'day') }} / 7 + case when date_part('dow', ({{second_date}})::timestamp) < date_part('dow', ({{first_date}})::timestamp) then 1 else 0 end)
{% elif datepart == 'week' %}
({{ dbt_utils.datediff(first_date, second_date, 'day') }} / 7 + case when date_part('dow', ({{second_date}})::timestamp) > date_part('dow', ({{first_date}})::timestamp) then 1 else 0 end)
({{ dbt_utils.datediff(first_date, second_date, 'day') }} / 7 + case
when date_part('dow', ({{first_date}})::timestamp) <= date_part('dow', ({{second_date}})::timestamp) then
case when {{first_date}} <= {{second_date}} then 0 else -1 end
else
case when {{first_date}} <= {{second_date}} then 1 else 0 end
end)
{% elif datepart == 'hour' %}
({{ dbt_utils.datediff(first_date, second_date, 'day') }} * 24 + date_part('hour', ({{second_date}})::timestamp) - date_part('hour', ({{first_date}})::timestamp))
{% elif datepart == 'minute' %}
Expand Down

0 comments on commit 7871a21

Please sign in to comment.