-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.sql
113 lines (103 loc) · 3.6 KB
/
views.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
CREATE VIEW CourseView AS
SELECT * FROM Courses;
/* StudentsFollowing */
CREATE VIEW StudentsFollowing AS
SELECT student_id, student_name, branch, programme_abbreviation
FROM Students;
/* Finished Courses */
CREATE VIEW FinishedCourses AS
SELECT hC.student_id, hC.code, hC.grade, Courses.credit
FROM hasCompleted hC
INNER JOIN Courses ON hC.code = Courses.code;
/* Registrations */
CREATE VIEW Registrations AS
SELECT student_id, code, 'Registered' AS status
FROM hasRegistered
UNION
SELECT student_id, code, 'Waiting' AS status
FROM isWaiting;
/* PassedCourses */
CREATE VIEW PassedCourses AS
SELECT hC.student_id, hC.code, hC.grade, Courses.credit
FROM hasCompleted hC
INNER JOIN Courses ON hC.code = Courses.code
WHERE (hC.grade != 'U');
/* UnreadMandatory */
CREATE VIEW UnreadMandatory AS
SELECT s.student_id, bM.code
FROM Students s
INNER JOIN branchMandatory bM ON s.programme_abbreviation = bM.programme_abbreviation AND s.branch = bM.branch
UNION
SELECT s.student_id, pM.code
FROM Students s
INNER JOIN programmeMandatory pM ON s.programme_abbreviation = pM.programme_abbreviation
MINUS
SELECT student_id, code
FROM hasCompleted hC
WHERE hC.grade != 'U';
/* PathToGraduation */
CREATE VIEW pathToGraduationHelper AS
SELECT s.student_id, pC.code, pC.credit FROM StudentsFollowing s
INNER JOIN PassedCourses pC ON s.student_id = pC.student_id
INNER JOIN branchMandatory bM ON s.programme_abbreviation = bM.programme_abbreviation
AND s.branch = bM.branch AND pC.code = bM.code;
CREATE VIEW PathToGraduation AS
WITH
Credits AS (
SELECT pC.student_id, SUM(pC.credit) AS Credits
FROM PassedCourses pC
INNER JOIN Courses ON pC.code = Courses.code
GROUP BY student_id
),
Credits_Branch AS (
SELECT pGH.student_id, SUM(pGH.credit) AS Credits_Branch
FROM pathToGraduationHelper pGH
GROUP BY student_id
),
Mandatory AS (
SELECT uM.student_id, COUNT(uM.code) AS Mandatory
FROM UnreadMandatory uM
GROUP BY student_id
),
Seminars AS (
SELECT pC.student_id, COUNT(pC.code) AS Seminars
FROM PassedCourses pC
INNER JOIN hasClassifications hC ON pC.code = hC.code AND hC.classification = 'Seminar'
INNER JOIN Courses ON pC.code = Courses.code
GROUP BY student_id
),
Credits_Math AS (
SELECT pC.student_id, SUM(pC.credit) AS Credits_Math
FROM PassedCourses pC
INNER JOIN hasClassifications hC ON pC.code = hC.code AND hC.classification = 'Math'
GROUP BY student_id
),
Credits_Research AS (
SELECT pC.student_id, SUM(pC.credit) AS Credits_Research
FROM PassedCourses pC
INNER JOIN hasClassifications hC ON pC.code = hC.code AND hC.classification = 'Research'
GROUP BY student_id
)
SELECT stud.student_id AS Student,
NVL(Credits, '0') AS Credits,
NVL(Credits_Branch, '0') AS Credits_Branch,
NVL(Mandatory, '0') AS Mandatory,
NVL(Seminars, '0') AS Seminars,
NVL(Credits_Math, '0') AS Credits_Math,
NVL(Credits_Research, '0') AS Credits_Research,
(CASE WHEN
Credits_Math >= 20
AND Credits_Research >= 10
AND Seminars >= 1
AND Mandatory IS NULL
THEN 'Done'
ELSE 'Not done'
END)
AS Status
FROM Students stud
LEFT JOIN Credits ON stud.student_id = Credits.student_id
LEFT JOIN Credits_Branch ON stud.student_id = Credits_Branch.student_id
LEFT JOIN Mandatory ON stud.student_id = Mandatory.student_id
LEFT JOIN Seminars ON stud.student_id = Seminars.student_id
LEFT JOIN Credits_Math ON stud.student_id = Credits_Math.student_id
LEFT JOIN Credits_Research ON stud.student_id = Credits_Research.student_id;