forked from get-select/dbt-snowflake-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily_rates.sql
226 lines (208 loc) · 6.65 KB
/
daily_rates.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{{ config(materialized='table') }}
/*
snowflake.organization_usage.rate_sheet_daily isn't guaranteed to have 1 row per day per usage type.
If you don't consume any compute resources on a given day, there won't be a record.
This model guarantees 1 row per day per usage type, by filling in missing values with rates from the last
known day.
*/
with
dates_base as (
select date_day as date from (
{{ dbt_utils.date_spine(
datepart="day",
start_date="'2018-01-01'",
end_date="dateadd(day, 1, current_date)"
)
}}
)
),
rate_sheet_daily_base as (
select
organization_name,
account_name,
account_locator,
date,
usage_type,
currency,
effective_rate,
service_type
from {{ ref('stg_rate_sheet_daily') }}
{% if not var('uses_org_view', false) %}
where
account_locator = {{ account_locator() }}
{% endif %}
),
stop_thresholds as (
select min(date) as start_date
from rate_sheet_daily_base
union all
select min(date) as start_date
from {{ ref('remaining_balance_daily_without_contract_view') }}
),
date_range as (
select
max(start_date) as start_date,
current_date as end_date
from stop_thresholds
),
remaining_balance_daily as (
select
date,
free_usage_balance + capacity_balance + on_demand_consumption_balance + rollover_balance as remaining_balance,
remaining_balance < 0 as is_account_in_overage
from {{ ref('remaining_balance_daily_without_contract_view') }}
),
latest_remaining_balance_daily as (
select
date,
remaining_balance,
is_account_in_overage
from remaining_balance_daily
qualify row_number() over (
order by date desc) = 1
),
rate_sheet_daily as (
select rate_sheet_daily_base.*
from rate_sheet_daily_base
inner join date_range
on rate_sheet_daily_base.date between date_range.start_date and date_range.end_date
),
rates_date_range_w_usage_types as (
select
date_range.start_date,
date_range.end_date,
{% if var('uses_org_view', false) %}
usage_types.account_locator,
{% endif %}
usage_types.usage_type
from date_range
cross join (select distinct rate_sheet_daily.account_locator, rate_sheet_daily.usage_type from rate_sheet_daily) as usage_types
),
base as (
select
db.date,
dr.usage_type,
{% if var('uses_org_view', false) %}
dr.account_locator,
{% endif %}
from dates_base as db
inner join rates_date_range_w_usage_types as dr
on db.date between dr.start_date and dr.end_date
),
rates_w_overage as (
select
base.date,
base.usage_type,
{% if var('uses_org_view', false) %}
base.account_locator,
{% endif %}
coalesce(
rate_sheet_daily.service_type,
lag(rate_sheet_daily.service_type) ignore nulls over (
partition by base.usage_type
{% if var('uses_org_view', false) %}
, base.account_locator
{% endif %}
order by base.date
),
lead(rate_sheet_daily.service_type) ignore nulls over (
partition by base.usage_type
{% if var('uses_org_view', false) %}
, base.account_locator
{% endif %}
order by base.date
)
) as service_type,
coalesce(
rate_sheet_daily.effective_rate,
lag(rate_sheet_daily.effective_rate) ignore nulls over (
partition by base.usage_type
{% if var('uses_org_view', false) %}
, base.account_locator
{% endif %}
order by base.date
),
lead(rate_sheet_daily.effective_rate) ignore nulls over (
partition by base.usage_type
{% if var('uses_org_view', false) %}
, base.account_locator
{% endif %}
order by base.date
)
) as effective_rate,
coalesce(
rate_sheet_daily.currency,
lag(rate_sheet_daily.currency) ignore nulls over (
partition by base.usage_type
{% if var('uses_org_view', false) %}
, base.account_locator
{% endif %}
order by base.date
),
lead(rate_sheet_daily.currency) ignore nulls over (
partition by base.usage_type
{% if var('uses_org_view', false) %}
, base.account_locator
{% endif %}
order by base.date
)
) as currency,
base.usage_type like 'overage-%' as is_overage_rate,
replace(base.usage_type, 'overage-', '') as associated_usage_type,
coalesce(remaining_balance_daily.is_account_in_overage, latest_remaining_balance_daily.is_account_in_overage, false) as _is_account_in_overage,
case
when _is_account_in_overage and is_overage_rate then 1
when not _is_account_in_overage and not is_overage_rate then 1
else 0
end as rate_priority
from base
left join latest_remaining_balance_daily on latest_remaining_balance_daily.date is not null
left join remaining_balance_daily
on base.date = remaining_balance_daily.date
left join rate_sheet_daily
on base.date = rate_sheet_daily.date
and base.usage_type = rate_sheet_daily.usage_type
{% if var('uses_org_view', false) %}
and base.account_locator = rate_sheet_daily.account_locator
{% endif %}
),
rates as (
select
date,
{% if var('uses_org_view', false) %}
account_locator,
{% endif %}
usage_type,
associated_usage_type,
service_type,
effective_rate,
currency,
is_overage_rate
from rates_w_overage
qualify row_number() over (
partition by date, service_type, associated_usage_type
{% if var('uses_org_view', false) %}
, account_locator
{% endif %}
order by rate_priority desc
) = 1
)
select
date,
{% if var('uses_org_view', false) %}
account_locator,
{% endif %}
associated_usage_type as usage_type,
service_type,
effective_rate,
currency,
is_overage_rate,
row_number() over (
partition by service_type, associated_usage_type
{% if var('uses_org_view', false) %}
, account_locator
{% endif %}
order by date desc
) = 1 as is_latest_rate
from rates
order by date