From 91cef11975a11b8afba773149f451a2517ad8990 Mon Sep 17 00:00:00 2001 From: Tristan Handy Date: Wed, 8 Feb 2017 17:34:06 -0500 Subject: [PATCH] fixed coupons model to eliminate duplicates --- models/transform/stripe_coupons.sql | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/models/transform/stripe_coupons.sql b/models/transform/stripe_coupons.sql index 9166abf..d4d7f59 100644 --- a/models/transform/stripe_coupons.sql +++ b/models/transform/stripe_coupons.sql @@ -2,17 +2,11 @@ with events as ( select * from {{ref('stripe_coupon_events')}} -), deleted_coupons as ( - - select id, created_at as deleted_at - from events - where event_type = 'coupon.deleted' - ) select distinct events.id, - first_value(created_at) over + first_value(case when event_type != 'coupon.deleted' then created_at end) over (partition by events.id order by created_at rows between unbounded preceding and unbounded following) as created_at, last_value(currency) over @@ -39,7 +33,7 @@ select distinct last_value(valid) over (partition by events.id order by created_at rows between unbounded preceding and unbounded following) as valid, - deleted_coupons.deleted_at + last_value(case when event_type = 'coupon.deleted' then created_at end ignore nulls) over + (partition by events.id order by created_at rows between unbounded preceding and unbounded following) + as deleted_at from events - left outer join deleted_coupons on events.id = deleted_coupons.id -where event_type != 'coupon.deleted'