Skip to content

Instantly share code, notes, and snippets.

@makoru-hikage
Last active February 1, 2017 07:43
Show Gist options
  • Save makoru-hikage/1a00172ab2fbacee09a6551b8c4db1d1 to your computer and use it in GitHub Desktop.
Save makoru-hikage/1a00172ab2fbacee09a6551b8c4db1d1 to your computer and use it in GitHub Desktop.
Groups all the students in the same section in a given academic term
/**
* INVOLVED TABLES
*
* TABLE: students
* id
* user_id
* degree_id
* ...
*
* TABLE: degrees
* id
* code varchar
* name varchar
* major varchar
*
* TABLE: term_attendance
* id
* student_id (the one who attended)
* school_calendar_id (the semester/term attended)
* year_level int (by the time of attendance)
* section varchar (by the time of attendance)
*
* TABLE: school_calendar
* (the table of semesters/terms that have passed or is passing)
* id
* part_number int (show the term be a semester, 1 means first semester)
* date_start datetime
* date_end datetime
*
* [NOTE: No need to make another column to specify what
* type of term (e.g. "semestral", "trimestral", "quarterly")
* to determine the type, just calculate the difference
* of date_start and date_end. Should it result to approximately
* 6 months, it is a semester]
*/
@exampleDegree = 'BSIT' --stands for BS in Info Tech
SELECT
year_level,
section,
COUNT(*)
FROM students
INNER JOIN term_attendance
ON term_attendance.student_id = students.id
WHERE student.degree_id = (SELECT id FROM degrees WHERE code = 'BSIT')
GROUP BY year_level, section, school_calendar_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment