forked from openedx/tutor-contrib-aspects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fact_learner_problem_summary.sql
129 lines (128 loc) · 3 KB
/
fact_learner_problem_summary.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
WITH problem_responses AS (
{% include 'openedx-assets/queries/int_problem_responses.sql' %}
), outcomes AS (
SELECT
emission_time,
org,
course_key,
problem_id,
actor_id,
success,
first_value(success) OVER (PARTITION BY course_key, problem_id, actor_id ORDER BY success DESC) AS was_successful
FROM problem_responses
), successful_responses AS (
SELECT
org,
course_key,
problem_id,
actor_id,
min(emission_time) AS first_success_at
FROM outcomes
WHERE was_successful = true and success = true
GROUP BY
org,
course_key,
problem_id,
actor_id
), unsuccessful_responses AS (
SELECT
org,
course_key,
problem_id,
actor_id,
max(emission_time) AS last_response_at
FROM outcomes
WHERE was_successful = false
GROUP BY
org,
course_key,
problem_id,
actor_id
), final_responses AS (
SELECT
org,
course_key,
problem_id,
actor_id,
first_success_at AS emission_time
FROM successful_responses
UNION ALL
SELECT
org,
course_key,
problem_id,
actor_id,
last_response_at AS emission_time
FROM unsuccessful_responses
), int_problem_results AS (
SELECT
emission_time,
org,
course_key,
course_name,
course_run,
problem_id,
problem_name,
actor_id,
responses,
success,
attempts
FROM problem_responses
INNER JOIN final_responses USING (org, course_key, problem_id, actor_id, emission_time)
), summary AS (
SELECT
org,
course_key,
course_name,
course_run,
problem_name,
actor_id,
success,
attempts,
0 AS num_hints_displayed,
0 AS num_answers_displayed
FROM int_problem_results
UNION ALL
SELECT
org,
course_key,
course_name,
course_run,
problem_name,
actor_id,
NULL AS success,
NULL AS attempts,
caseWithExpression(help_type, 'hint', 1, 0) AS num_hints_displayed,
caseWithExpression(help_type, 'answer', 1, 0) AS num_answers_displayed
FROM {{ DBT_PROFILE_TARGET_DATABASE }}.int_problem_hints
WHERE
1=1
{% include 'openedx-assets/queries/common_filters.sql' %}
)
SELECT
org,
course_key,
course_name,
course_run,
problem_name,
actor_id,
coalesce(any(success), false) AS success,
coalesce(any(attempts), 0) AS attempts,
sum(num_hints_displayed) AS num_hints_displayed,
sum(num_answers_displayed) AS num_answers_displayed
FROM summary
where
{% raw %}
{% if filter_values('problem_name') != [] %}
problem_name in {{ filter_values('problem_name') | where_in }}
{% else %}
1=0
{% endif %}
{% endraw %}
GROUP BY
org,
course_key,
course_name,
course_run,
problem_name,
actor_id