-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel-3
More file actions
87 lines (71 loc) · 3.31 KB
/
Level-3
File metadata and controls
87 lines (71 loc) · 3.31 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
/* Level 3
-------
A college needs to develop a system to allocate Open Elective Subjects to its respective
students. The way the system would work is that each student is allowed 5 choices with the
respective preference, where number 1 indicates the first preference, number 2 indicates
second preference and so on, the subjects are supposed to be allotted on the basis of the
Student’s GPA, which means the student with the students with the highest GPAs are
allotted the subject they want. Every subject has a limited number of seats so if a subject
has 60 seats and all of them are filled then the student would not be allotted his first
preference but instead second would be checked, if the second preference is full as well
then the third preference would be checked, this process would be repeated till the student
is allotted a subject of his/her choice. If in case all the preferences that the student has
selected are already full, then the student would be considered as unallotted and would be
marked so.
*/
CREATE PROCEDURE ElectiveSubjectAllocation
AS
BEGIN
-- Delete all the previous allotments and unallotted students from the table.
TRUNCATE TABLE Allotments;
TRUNCATE TABLE UnallotedStudents;
-- Iterate through each student
DECLARE @StudentID INT;
DECLARE @SubjectID VARCHAR(10);
DECLARE @Preference INT;
DECLARE @GPA DECIMAL(3, 1);
DECLARE @RemainingSeats INT;
DECLARE student_cursor CURSOR FOR
SELECT sp.StudentID, sp.SubjectID, sp.Preference, s.GPA
FROM StudentPreference sp
INNER JOIN StudentDetails s ON sp.StudentID = s.StudentID
ORDER BY sp.StudentID, sp.Preference;
OPEN student_cursor;
FETCH NEXT FROM student_cursor INTO @StudentID, @SubjectID, @Preference, @GPA;
WHILE @@FETCH_STATUS = 0
BEGIN
--Check whether the student is already allotted or not.
IF NOT EXISTS (SELECT 1 FROM Allotments WHERE StudentID = @StudentID)
BEGIN
--Choose the subject on the basis of preference and available seats.
WHILE @Preference <= 5
BEGIN
SELECT @RemainingSeats = RemainingSeats
FROM SubjectDetails
WHERE SubjectID = @SubjectID;
IF @RemainingSeats > 0
BEGIN
INSERT INTO Allotments (SubjectID, StudentID)
VALUES (@SubjectID, @StudentID);
UPDATE SubjectDetails
SET RemainingSeats = RemainingSeats - 1
WHERE SubjectID = @SubjectID;
BREAK;
END
-- If the subject is full, then check for the next preference.
FETCH NEXT FROM student_cursor INTO @StudentID, @SubjectID, @Preference, @GPA;
END
--If all the preferences are full, then insert the StudentId in UnallottedStudents table.
IF NOT EXISTS (SELECT 1 FROM Allotments WHERE StudentID = @StudentID)
BEGIN
INSERT INTO UnallotedStudents (StudentID)
VALUES (@StudentID);
END
END
--Again check for the next student
FETCH NEXT FROM student_cursor INTO @StudentID, @SubjectID, @Preference, @GPA;
END
CLOSE student_cursor;
DEALLOCATE student_cursor;
END;
EXEC ElectiveSubjectAllocation;