-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_fct_prices_model.sql
53 lines (38 loc) · 1.11 KB
/
get_fct_prices_model.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{% macro get_fct_prices_model(vehicle_type) %}
{% set primary_key = 'id' %}
{% set vehicle_type_plural = vehicle_type + 's' %}
with {{ vehicle_type_plural }} as (
select *,
valid_to::date as valid_to_date,
valid_from::date as valid_from_date
from {{ ref('stg_' + vehicle_type_plural + '_snapshots') }}
),
dates as (
select * from {{ ref('dim_date') }}
),
deduplicated as (
select
*
from {{ vehicle_type_plural }}
qualify row_number() over (partition by {{ primary_key }}, valid_from_date order by version desc) = 1
),
spined as (
select
deduplicated.{{ primary_key }},
deduplicated.scd_id,
deduplicated.price,
dates.date_key
from deduplicated
left join dates
on dates.date_key >= deduplicated.valid_from_date
and dates.date_key < deduplicated.valid_to_date
where dates.date_key <= (select max(creation_date)::date from deduplicated)
and dates.date_key >= (select min(creation_date)::date from deduplicated)
)
select
date_key,
{{ primary_key }},
scd_id,
price
from spined
{% endmacro %}