duplicate athena verifications

This commit is contained in:
Pablo Martin 2024-10-04 16:29:57 +02:00
parent 67b8e1263d
commit 30c73b1ab9
3 changed files with 61 additions and 14 deletions

View file

@ -0,0 +1,34 @@
{% set ok_status = "Approved" %}
-- 2GBP/booked night if booking is approved, to be charged on checkout
{% set cost_per_night = 2 %}
with
int_athena__verifications as (select * from {{ ref("int_athena__verifications") }}),
-- CTE to rank verifications by updated_at_utc per id_booking
ranked_verifications as (
select
v.*,
row_number() over (
partition by v.id_booking order by v.updated_at_utc asc
) as rn
from int_athena__verifications v
where v.version = 'V1' and v.id_booking is not null
)
select
v.id_verification,
v.id_booking,
v.verification_status,
v.is_cancelled,
-- Charge for 1 night if number_nights = 0
case
when v.number_nights = 0 and v.verification_status = '{{ ok_status }}'
then {{ cost_per_night }}
when v.verification_status = '{{ ok_status }}'
then v.number_nights * {{ cost_per_night }}
else 0
end as ok_status_fee_in_gbp,
v.created_date_utc,
v.checkout_date_utc
from ranked_verifications v
where
-- Select only the most recent verification for each id_booking
v.rn = 1