Last active
January 9, 2017 10:50
-
-
Save makoru-hikage/c302cb59ef43c2ebc29d6415a54faaa5 to your computer and use it in GitHub Desktop.
This is an SQL query for extraction of semesters and any other terms under a school year.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Assuming the the table design is like this | |
-- | |
-- Table Name: school_calendar | |
-- Columns | |
-- id - INT NOT NULL PRIMARY KEY | |
-- semester - VARCHAR (255) NOT NULL | |
-- date_start - DATE | |
-- date_end - DATE | |
-- | |
-- The data will be as follows | |
INSERT INTO school_calendars (semester, date_start, date_end) VALUES | |
-- 1st Semester of SY 2012-2013 | |
('1st', '2012-06-01', '2012-10-30'), | |
-- 2nd Semester of SY 2012-2013 | |
('2nd', '2012-11-03', '2013-05-30'), | |
-- 1st Semester of SY 2013-2014 | |
('1st', '2013-06-01', '2013-10-30'), | |
-- 2nd Semester of SY 2013-2014 | |
('2nd', '2013-11-03', '2014-05-30'); | |
--See how the data were inserted, no "2012-2013" was inserted | |
--To extract the data we must first assign a variable | |
SET @sy = 2012; | |
SET @sem = 2nd; | |
--Then we shall execute the query: | |
SELECT semester, date_start, date_end FROM school_calendar | |
WHERE | |
(YEAR(date_start) = @sy) | |
AND (YEAR(date_end) BETWEEN @sy AND (@sy+1)) | |
-- It shall yield two results: The two semesters in one school year. | |
--You can modify the code to yield "2012-2013, 2nd [Semester]" | |
SELECT CONCAT(YEAR(MIN(date_start)),'-',YEAR(MAX(date_end)),', ', semester) AS `Sem/SY Taken` FROM school_calendar | |
WHERE | |
(YEAR(date_start) = @sy) | |
AND (YEAR(date_end) BETWEEN @sy AND (@sy+1)) | |
AND semester = @sem; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment