data-dwh-dbt-project/models/intermediate/athena/int_athena__verifications_with_fees.sql

44 lines
1.5 KiB
MySQL
Raw Normal View History

{% set ok_status = "Approved" %}
2024-09-04 17:24:55 +02:00
with
2024-10-04 16:29:57 +02:00
int_athena__verifications as (select * from {{ ref("int_athena__verifications") }}),
stg_seed__athena_price_history as (
select * from {{ ref("stg_seed__athena_price_history") }}
),
-- 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
2024-10-04 16:29:57 +02:00
from int_athena__verifications v
where v.version = 'V1' and v.id_booking is not null
2024-09-04 17:24:55 +02:00
)
select
v.id_verification,
v.id_booking,
2024-09-05 17:21:48 +02:00
v.verification_status,
2024-09-04 17:24:55 +02:00
v.is_cancelled,
-- Charge for 1 night if number_nights = 0
case
2024-09-05 17:21:48 +02:00
when v.number_nights = 0 and v.verification_status = '{{ ok_status }}'
then ph.fee_per_night_gbp
2024-09-05 17:21:48 +02:00
when v.verification_status = '{{ ok_status }}'
then v.number_nights * ph.fee_per_night_gbp
2024-09-05 17:21:48 +02:00
else 0
end as ok_status_fee_in_gbp,
2024-09-04 17:24:55 +02:00
v.created_date_utc,
v.checkin_date_utc,
2024-09-04 17:24:55 +02:00
v.checkout_date_utc
from ranked_verifications v
left join
stg_seed__athena_price_history ph
-- The following condition ensures avoiding duplicates.
-- Keep in mind that the start_at_utc is inclusive to the price,
-- while the end_at_utc is exclusive.
on v.checkout_at_utc >= ph.start_at_utc
and v.checkout_at_utc < ph.end_at_utc
where
-- Select only the most recent verification for each id_booking
v.rn = 1