-
Notifications
You must be signed in to change notification settings - Fork 1
/
export_survey_data.sql
302 lines (286 loc) · 9.93 KB
/
export_survey_data.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
-- Response rate
WITH given_num_families AS
(
SELECT ROW_NUMBER() OVER () AS order_by,
column1 AS num_families_at_school,
column2 AS grade_level
FROM (
-- Fill these in based on values from the front office.
-- Note that the name given in the second value must match what's in the `base` CTE.
VALUES (408, 'total'),
(NULL, 'grammar'),
(NULL, 'middle'),
(NULL, 'high')
) AS provided_by_front_office
),
base AS
(
SELECT COUNT(0) AS max_families_responding,
SUM(num_individuals_in_response) / 2. AS min_families_responding,
'total' AS grade_level
FROM respondents
WHERE NOT soft_delete
UNION ALL
SELECT COUNT(0) AS max_families_responding,
SUM(num_individuals_in_response) / 2. AS min_families_responding,
'grammar' AS grade_level
FROM respondents
WHERE NOT soft_delete
AND grammar_avg IS NOT NULL
UNION ALL
SELECT COUNT(0) AS max_families_responding,
SUM(num_individuals_in_response) / 2. AS min_families_responding,
'middle' AS grade_level
FROM respondents
WHERE NOT soft_delete
AND middle_avg IS NOT NULL
UNION ALL
SELECT COUNT(0) AS max_families_responding,
SUM(num_individuals_in_response) / 2. AS min_families_responding,
'high' AS grade_level
FROM respondents
WHERE NOT soft_delete
AND high_avg IS NOT NULL
),
metrics AS
(
SELECT grade_level,
max_families_responding,
min_families_responding,
(max_families_responding + min_families_responding) / 2. AS approx_families_responding
FROM base
),
percentages AS
(
SELECT order_by,
grade_level,
max_families_responding,
min_families_responding,
approx_families_responding,
num_families_at_school AS out_of,
ROUND(approx_families_responding / num_families_at_school, 3) AS response_pct
FROM metrics
JOIN
given_num_families USING (grade_level)
)
SELECT grade_level, max_families_responding, min_families_responding, approx_families_responding, out_of, response_pct
FROM percentages
ORDER BY order_by
;
--respondents
SELECT respondent_id,
start_datetime,
end_datetime,
num_individuals_in_response,
tenure,
minority,
any_support,
grammar_avg,
middle_avg,
high_avg,
overall_avg,
collector_id,
collector_description
FROM respondents
LEFT JOIN
collectors USING (collector_id)
WHERE NOT soft_delete
ORDER BY respondent_id
;
-- questions
SELECT *
FROM questions
;
-- responses_rank
WITH all_respondent_questions AS
(
SELECT respondent_id,
question_id,
question_text
FROM respondents
CROSS JOIN
questions
WHERE question_type = 'rank'
AND NOT soft_delete
)
SELECT respondent_id,
question_id,
question_text,
CASE
WHEN grammar THEN 'Grammar'
WHEN middle THEN 'Middle'
WHEN high THEN 'High'
END AS grade_level,
response_value,
response_text
FROM all_respondent_questions
LEFT JOIN
question_rank_responses USING (respondent_id, question_id)
LEFT JOIN
question_response_mapping USING (question_id, response_value)
ORDER BY respondent_id, question_id, grammar DESC, middle DESC, high DESC
;
-- response_open
WITH all_respondent_questions AS
(
SELECT respondent_id,
question_id,
question_text
FROM respondents
CROSS JOIN
questions
WHERE question_type = 'open response'
AND NOT soft_delete
)
SELECT respondent_id,
question_id,
question_text,
CASE
WHEN grammar THEN 'Grammar'
WHEN middle THEN 'Middle'
WHEN high THEN 'High'
WHEN whole_school THEN 'Whole School'
END AS grade_level,
response
FROM all_respondent_questions
LEFT JOIN
question_open_responses USING (respondent_id, question_id)
ORDER BY respondent_id, question_id, grammar DESC, middle DESC, high DESC, whole_school DESC
;
-- flattened respondent_rank_questions
WITH duplicated_respondents AS
(
SELECT respondent_id,
tenure = 1 AS new_family,
minority,
any_support,
grammar_avg IS NOT NULL AS grammar_respondent,
middle_avg IS NOT NULL AS middle_respondent,
high_avg IS NOT NULL AS high_respondent,
overall_avg AS avg_score,
soft_delete
FROM respondents
WHERE NOT soft_delete
UNION ALL
-- Get a second rows for any respondent which represented two people
SELECT respondent_id,
tenure = 1 AS new_family,
minority,
any_support,
grammar_avg IS NOT NULL AS grammar_respondent,
middle_avg IS NOT NULL AS middle_respondent,
high_avg IS NOT NULL AS high_respondent,
overall_avg AS avg_score,
soft_delete
FROM respondents
WHERE NOT soft_delete
AND num_individuals_in_response = 2
),
all_respondent_questions AS
(
SELECT respondent_id,
question_id,
question_text
FROM respondents
CROSS JOIN
questions
WHERE question_type = 'rank'
AND NOT soft_delete
),
rank_questions AS
(
SELECT respondent_id,
question_id,
question_text,
CASE
WHEN grammar THEN 'Grammar'
WHEN middle THEN 'Middle'
WHEN high THEN 'High'
END AS grade_level_for_response,
response_value,
response_text
FROM all_respondent_questions
LEFT JOIN
question_rank_responses USING (respondent_id, question_id)
LEFT JOIN
question_response_mapping USING (question_id, response_value)
)
SELECT -- respondents
respondent_id,
new_family,
minority,
any_support,
grammar_respondent,
middle_respondent,
high_respondent,
avg_score,
-- questions
question_id,
question_text,
grade_level_for_response,
response_value,
response_text
FROM rank_questions
JOIN
duplicated_respondents USING (respondent_id)
ORDER BY respondent_id, question_id, grade_level_for_response
;
-- flattened respondent_rank_questions ****for 2022 only****
WITH respondents_expanded AS
(
SELECT respondent_id,
tenure = 1 AS new_family,
minority,
any_support,
grammar_avg IS NOT NULL AS grammar_respondent,
NULL AS middle_respondent,
upper_avg IS NOT NULL AS upper_respondent,
overall_avg AS avg_score
FROM gvca_survey.sac_survey_2022.respondents
),
all_respondent_questions AS
(
SELECT respondent_id,
question_id,
question_text
FROM respondents_expanded
CROSS JOIN
sac_survey_2022.question
WHERE question_id < 9
AND question_id > 2
),
rank_questions AS
(
SELECT respondent_id,
question_id,
question_text,
CASE
WHEN grammar THEN 'Grammar'
WHEN upper THEN 'Upper'
WHEN NOT grammar AND NOT upper AND question_id = 7
THEN '"Grade level" not used for this question'
END AS grade_level_for_response,
response
FROM all_respondent_questions
LEFT JOIN
sac_survey_2022.question_rank USING (respondent_id, question_id)
)
SELECT -- respondents
respondent_id,
new_family,
minority,
any_support,
grammar_respondent,
middle_respondent,
upper_respondent,
avg_score,
-- questions
question_id,
question_text,
grade_level_for_response,
response
FROM rank_questions
JOIN
respondents_expanded USING (respondent_id)
ORDER BY respondent_id, question_id, grade_level_for_response
;