From 37e8e0c2983d9f157abed4f8a9ca2c7e1238587d Mon Sep 17 00:00:00 2001 From: PavelBegunkov <asml.Silence@gmail.com> Date: Sun, 30 Nov 2014 18:00:45 +0300 Subject: [PATCH] reports sql --- db/StoredProcedures.sql | 105 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql index effd6e434..298771938 100644 --- a/db/StoredProcedures.sql +++ b/db/StoredProcedures.sql @@ -3228,14 +3228,19 @@ CREATE FUNCTION `GetRateForDisc` ( `StudentID` INT, ) RETURNS int(11) NO SQL BEGIN - RETURN ( SELECT SUM(rating_table.Rate) - FROM `rating_table` - INNER JOIN `submodules` ON rating_table.SubmoduleID = submodules.ID - INNER JOIN `modules` ON submodules.ModuleID = modules.ID AND - DisciplineID = modules.DisciplineID - WHERE rating_table.StudentID = StudentID - LIMIT 1 - ); + DECLARE rate; + SET rate = -1; + + SELECT SUM(rating_table.Rate) + INTO rate + FROM `rating_table` + INNER JOIN `submodules` ON rating_table.SubmoduleID = submodules.ID + INNER JOIN `modules` ON submodules.ModuleID = modules.ID AND + DisciplineID = modules.DisciplineID + WHERE rating_table.StudentID = StudentID + LIMIT 1; + + RETURN rate; END // @@ -3656,4 +3661,88 @@ BEGIN END // + + + + +DROP PROCEDURE IF EXISTS GetReports// +CREATE PROCEDURE `GetReports` ( IN `StudyGroupID` INT + ) + NO SQL +BEGIN + SELECT students.ID AS 'StudentID', + students.LastName, + students.FirstName, + students.SecondName, + disciplines.ID, + GetRateForDisc(students.ID, disciplines.ID) AS 'Rate', + GetMaxRateForDisc(disciplines.ID) AS 'MaxRate' + FROM `students` + LEFT JOIN `disciplines` ON disciplines.ID IN + ( + SELECT disciplines_groups.DisciplineID + FROM `disciplines_groups` + WHERE disciplines_groups.StudyGroupID = StudyGroupID + UNION DISTINCT + SELECT disciplines_students.DisciplineID + FROM `disciplines_students` + INNER JOIN `students` ON disciplines_students.ID = students.ID + WHERE students.StudyGroupID = StudyGroupID + ) + INNER JOIN `subjects` ON disciplines.SubjectID = subjects.ID + WHERE students.StudyGroupID = StudyGroupID; +END // + + + + +-- DROP PROCEDURE IF EXISTS GetReports// +-- CREATE PROCEDURE `GetReports` ( IN `StudyGroupID` INT +-- ) +-- NO SQL +-- BEGIN +-- -- CREATE TEMPORARY TABLE IF NOT EXISTS `temp_disciplines` ( +-- -- `DisciplineID` int(11) NOT NULL, +-- -- `MaxRate` int(11) NOT NULL DEFAULT '0', +-- -- PRIMARY KEY (`DisciplineID`) +-- -- ) ENGINE=MEMORY DEFAULT CHARSET=utf8 AS ( +-- -- SELECT disciplines_groups.DisciplineID, +-- -- 0 AS 'MaxRate' +-- -- FROM `disciplines_groups` +-- -- WHERE disciplines_groups.StudyGroupID = StudyGroupID +-- -- UNION DISTINCT +-- -- SELECT disciplines_students.DisciplineID, +-- -- 0 AS 'MaxRate' +-- -- FROM `disciplines_students` +-- -- INNER JOIN `students` ON disciplines_students.ID = students.ID +-- -- WHERE students.StudyGroupID = StudyGroupID +-- -- ); + +-- CREATE TEMPORARY TABLE IF NOT EXISTS `temp_disciplines` +-- SELECT disciplines_groups.DisciplineID, +-- 0 AS 'MaxRate' +-- FROM `disciplines_groups` +-- WHERE disciplines_groups.StudyGroupID = StudyGroupID +-- UNION DISTINCT +-- SELECT disciplines_students.DisciplineID, +-- 0 AS 'MaxRate' +-- FROM `disciplines_students` +-- INNER JOIN `students` ON disciplines_students.ID = students.ID +-- WHERE students.StudyGroupID = StudyGroupID; + +-- UPDATE `temp_disciplines` +-- SET temp_disciplines.MaxRate = GetMaxRateForDisc(temp_disciplines.DisciplineID); + +-- SELECT students.ID AS 'StudentID', +-- temp_disciplines.DisciplineID, +-- temp_disciplines.MaxRate, +-- GetRateForDisc(students.ID, temp_disciplines.DisciplineID) AS 'Rate' +-- FROM `students` +-- LEFT JOIN `temp_disciplines` ON 1=1 +-- WHERE students.StudyGroupID = StudyGroupID; +-- END // + + + + DELIMITER ; \ No newline at end of file -- GitLab