forked from openedx/tutor-contrib-aspects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfact_enrollments_by_day.sql
61 lines (61 loc) · 1.53 KB
/
fact_enrollments_by_day.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
with enrollments as (
{% include 'openedx-assets/queries/fact_enrollments.sql' %}
), enrollments_ranked as (
select
emission_time,
org,
course_key,
course_name,
course_run,
actor_id,
enrollment_mode,
enrollment_status,
rank() over (partition by date(emission_time), org, course_name, course_run, actor_id order by emission_time desc) as event_rank
from
enrollments
), enrollment_windows as (
select
org,
course_key,
course_name,
course_run,
actor_id,
enrollment_status,
enrollment_mode,
emission_time as window_start_at,
lagInFrame(emission_time, 1, now() + interval '1' day) over (partition by org, course_name, course_run, actor_id order by emission_time desc) as window_end_at
from
enrollments_ranked
where
event_rank = 1
), enrollment_window_dates as (
select
org,
course_key,
course_name,
course_run,
actor_id,
enrollment_status,
enrollment_mode,
date_trunc('day', window_start_at) as window_start_date,
date_trunc('day', window_end_at) as window_end_date
from enrollment_windows
)
select
date(fromUnixTimestamp(
arrayJoin(
range(
toUnixTimestamp(window_start_date),
toUnixTimestamp(window_end_date),
86400
)
)
)) as enrollment_status_date,
org,
course_key,
course_name,
course_run,
actor_id,
enrollment_status,
enrollment_mode
from enrollment_window_dates