-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_base_times.sql
60 lines (52 loc) · 1.63 KB
/
get_base_times.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
{% macro get_base_times(level='hour', base='1970-01-01') %}
{{ adapter.dispatch('get_base_times', 'dbt_resto') (level, base) }}
{% endmacro %}
{% macro default__get_base_times(level, base) %}
{%- if level == 'second' %}
{# 1 day = 86400 seconds #}
with E1(N) as
(
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all select 1
) --10E+1 or 10 rows
,E2(N) as
(
select 1 from E1 a, E1 b
) --10E+2 or 100 rows
,E3(N) as
(
select 1 from E2 a, E2 b
) --10E+4 or 10,000 rows
,E4(N) as
(
select 1 FROM E3 a, E3 b
) --10E+8 or 100,000,000 rows
,EALL as (
select {% if target.type == 'sqlserver' %} top 86400 {% endif %}
N,
row_number() over (order by N) as value
from E4
order by 2
{% if target.type != 'sqlserver' %} limit 86400 {% endif %}
)
select {{ dbt_resto.dateadd("second", "value", "'" ~ base ~ "'") }} as time_value
from EALL
{%- elif level == 'minute' %}
{# 1 day = 1440 minutes #}
{%- for value in range(0, 1440) %}
select {{ dbt_resto.dateadd("minute", value, "'" ~ base ~ "'") }} as time_value
{%- if not loop.last %}
union all
{%- endif %}
{%- endfor %}
{%- else %}
{# 1 day = 24 hours #}
{%- for value in range(0, 24) %}
select {{ dbt_resto.dateadd("hour", value, "'" ~ base ~ "'") }} as time_value
{%- if not loop.last %}
union all
{%- endif %}
{%- endfor %}
{%- endif %}
{% endmacro %}