-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudentInfoQuery.sql
More file actions
150 lines (148 loc) · 4.96 KB
/
Copy pathstudentInfoQuery.sql
File metadata and controls
150 lines (148 loc) · 4.96 KB
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
-- Active: 1725496082556@@127.0.0.1@5432@learnopsdev
SELECT * FROM get_students_by_cohortId (13)
DROP FUNCTION IF EXISTS get_students_by_cohortId (INTEGER);
CREATE FUNCTION get_students_by_cohortId(user_cohort_id INTEGER)
RETURNS TABLE (
user_id INTEGER,
student_name TEXT,
score INTEGER,
tags TEXT,
proposals TEXT,
book_id INTEGER,
book_name TEXT,
project_name TEXT,
book_index TEXT,
status_id INTEGER,
github_handle TEXT,
cohort_id INTEGER,
cohort_name TEXT,
break_start_date TEXT,
end_date TEXT,
briggs_myers_type TEXT
) AS $$
BEGIN
RETURN QUERY
WITH student_score AS (
SELECT
lr.student_id,
SUM(lw.weight) AS score
FROM "LearningAPI_learningrecord" AS lr
JOIN "LearningAPI_learningweight" AS lw ON lr.weight_id = lw.id
WHERE lr.achieved = TRUE
GROUP BY lr.student_id
),
student_name AS (
SELECT
nsu.user_id,
CONCAT(au.first_name, ' ', au.last_name) AS name,
nsu.github_handle
FROM "LearningAPI_nssuser" AS nsu
JOIN "auth_user" AS au ON au.id = nsu.user_id
),
student_tag AS (
SELECT
lst.student_id,
json_agg(
json_build_object(
'id', lst.tag_id,
'name', lt.name
)
)::text AS tags
FROM "LearningAPI_studenttag" AS lst
JOIN "LearningAPI_tag" AS lt ON lt.id = lst.tag_id
WHERE lst.tag_id IS NOT NULL
GROUP BY lst.student_id
),
student_proposals AS (
SELECT
u.id,
COALESCE(
json_agg(
json_build_object(
'proposal_id', cap.id,
'course_id', co.id,
'proposal_status',
CASE
WHEN ps.status = 'MVP' THEN 'mvp'
WHEN ps.status = 'Approved' THEN 'approved'
WHEN ps.status = 'In Review' THEN 'reviewed'
WHEN ps.status IS NULL THEN 'submitted'
END
)
)
, '[]')::text AS proposals
FROM "LearningAPI_nssuser" AS u
LEFT JOIN "LearningAPI_capstone" AS cap ON cap.student_id = u.id
LEFT JOIN "LearningAPI_capstonetimeline" AS tl
ON cap.id = tl.capstone_id
AND tl.id = (
SELECT id
FROM "LearningAPI_capstonetimeline"
WHERE cap.id = capstone_id
ORDER BY "date" DESC
LIMIT 1
)
LEFT JOIN "LearningAPI_proposalstatus" AS ps ON ps.id = tl.status_id
LEFT JOIN "LearningAPI_course" AS co ON co.id = cap.course_id
GROUP BY u.id
),
student_books AS (
SELECT
spr.student_id AS student_id,
bk.id AS book_id,
bk."name" AS book_name,
pr."name" AS project_name,
bk.index as book_index
FROM "LearningAPI_book" bk
JOIN "LearningAPI_project" pr ON pr.book_id = bk.id
JOIN "LearningAPI_studentproject" spr ON spr.project_id = pr.id
AND spr.id = (
SELECT id
FROM "LearningAPI_studentproject"
WHERE student_id = spr.student_id
ORDER BY "id" DESC
LIMIT 1
)
),
student_assessment_status AS (
SELECT
sa.student_id,
sa.status_id
FROM "LearningAPI_studentassessment" sa
WHERE sa."id" = (
SELECT MAX(id)
FROM "LearningAPI_studentassessment"
WHERE student_id = sa.student_id
)
)
SELECT
nu.user_id::int,
sn."name"::text AS student_name,
COALESCE(ss.score, 0)::int AS score,
COALESCE(st.tags, '[]'::text) as tags,
COALESCE(sp.proposals, '[]'::text) AS proposals,
sb.book_id::int,
sb.book_name::text,
sb.project_name::text,
sb.book_index::text,
COALESCE(sa.status_id, 0)::int,
nu.github_handle::text,
co.id::int AS cohort_id,
co."name"::text AS cohort_name,
co.break_start_date::text,
co.end_date::text,
stp.briggs_myers_type::text
FROM "LearningAPI_nssuser" nu
JOIN "LearningAPI_nssusercohort" nuc ON nu.user_id = nuc.nss_user_id
JOIN "LearningAPI_cohort" co ON co.id = nuc.cohort_id
LEFT JOIN student_score AS ss ON ss.student_id = nu.user_id
LEFT JOIN student_name AS sn ON sn.user_id = nu.user_id
LEFT JOIN student_tag AS st ON st.student_id = nu.user_id
LEFT JOIN student_proposals AS sp ON sp.id = nu.user_id
LEFT JOIN student_books AS sb ON sb.student_id = nu.user_id
LEFT JOIN student_assessment_status AS sa ON sa.student_id = nu.user_id
LEFT JOIN "LearningAPI_studentpersonality" stp ON stp.student_id = nu.user_id
WHERE nuc.cohort_id = user_cohort_id
ORDER BY nu.user_id;
END;
$$ LANGUAGE plpgsql;