-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q16.php
92 lines (76 loc) · 2.3 KB
/
Q16.php
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
<!DOCTYPE html>
<html>
<head>
<title>Q16</title>
<link rel="stylesheet" type="text/css" href="styles_Q.css">
</head>
<body>
<h1> Q16: compare the average GPA for each course between 2022F and 2023F.</h1>
<div class="notification">
<?php
$db_host = '127.0.0.1';
$db_user = 'root';
$db_password = 'root';
$db_db = 'INSY661Database';
$db_port = 8889;
$mysqli = new mysqli(
$db_host,
$db_user,
$db_password,
$db_db,
$db_port
);
if ($mysqli->connect_error) {
echo 'Errno: '.$mysqli->connect_errno;
echo '<br>';
echo 'Error: '.$mysqli->connect_error;
exit();
}
$sql = "
SELECT
s.dept_id,
c.course_id,
c.course_name,
COALESCE(ROUND(AVG(CASE WHEN o.sem_year = '2022F' THEN s.stud_gpa END), 2), 0) AS avg_gpa_2022F,
COALESCE(ROUND(AVG(CASE WHEN o.sem_year = '2023F' THEN s.stud_gpa END), 2), 0) AS avg_gpa_2023F
FROM
Study s
JOIN
Courses c ON s.dept_id = c.dept_id AND s.course_id = c.course_id
JOIN
Offered_by o ON s.dept_id = o.dept_id AND s.course_id = o.course_id
GROUP BY
s.dept_id,
c.course_id,
c.course_name
HAVING
avg_gpa_2022F > 0
AND
avg_gpa_2023F > 0
ORDER BY
s.dept_id,
c.course_id";
$res = $mysqli->query($sql);
echo '<table>';
echo '<tr>';
echo '<th>Department ID</th>';
echo '<th>Course ID</th>';
echo '<th>Course Name</th>';
echo '<th>Avg GPA 2022F</th>';
echo '<th>Avg GPA 2023F</th>';
echo '</tr>';
while ($row = $res->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row['dept_id'] . '</td>';
echo '<td>' . $row['course_id'] . '</td>';
echo '<td>' . $row['course_name'] . '</td>';
echo '<td>' . $row['avg_gpa_2022F'] . '</td>';
echo '<td>' . $row['avg_gpa_2023F'] . '</td>';
echo '</tr>';
}
echo '</table>';
$mysqli->close();
?>
</div>
</body>
</html>