-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCR_CURSOS_PERMITIDOS_CURSAR_ALUMNO.sql
41 lines (30 loc) · 1.47 KB
/
PCR_CURSOS_PERMITIDOS_CURSAR_ALUMNO.sql
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
DELIMITER $$
DROP PROCEDURE IF EXISTS `universidad`.`PCR_CURSOS_PERMITIDOS_CURSAR_ALUMNO` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `PCR_CURSOS_PERMITIDOS_CURSAR_ALUMNO`(IN ID_ALUMNO_PARAM BIGINT)
BEGIN
CREATE TEMPORARY TABLE CANTIDAD_ALUMNOS_INSCRIPTOS_ASIGNATURA
SELECT C.ID_ASIGNATURA AS ID_ASIGNATURA,C.ID_DOCENTE AS ID_DOCENTE,COUNT(*) AS CANTIDAD
FROM CURSOS C
LEFT JOIN INSCRIPCIONES I ON I.ID_DOCENTE = C.ID_DOCENTE AND I.ID_ASIGNATURA = C.ID_ASIGNATURA
GROUP BY C.ID_ASIGNATURA,C.ID_DOCENTE;
CREATE TEMPORARY TABLE MATERIAS_PERMITIDAS
SELECT C.ID_ASIGNATURA AS ID_ASIGNATURA, C.ID_DOCENTE AS ID_DOCENTE, CAI.CANTIDAD AS CANTIDAD_ALUMNOS FROM cursos c
INNER JOIN CANTIDAD_ALUMNOS_INSCRIPTOS_ASIGNATURA CAI ON (CAI.ID_ASIGNATURA = C.ID_ASIGNATURA AND CAI.ID_DOCENTE = C.ID_DOCENTE)
WHERE(
c.id_asignatura NOT IN (
SELECT id_asignatura FROM inscripciones
WHERE id_alumno = ID_ALUMNO_PARAM AND (estado = 1 OR estado = 2 ))
OR
c.id_docente NOT IN (
SELECT id_docente FROM inscripciones
WHERE id_alumno = ID_ALUMNO_PARAM AND (estado = 1 OR estado = 2 ))
);
SELECT C.* FROM MATERIAS_PERMITIDAS MP
INNER JOIN CURSOS C ON (C.ID_DOCENTE = MP.ID_DOCENTE AND C.ID_ASIGNATURA = MP.ID_ASIGNATURA)
INNER JOIN ASIGNATURAS A ON A.ID = MP.ID_ASIGNATURA
INNER JOIN DOCENTES D ON D.ID = MP.ID_DOCENTE
WHERE MP.CANTIDAD_ALUMNOS <= C.CUPOMAXIMO;
DROP TEMPORARY TABLE CANTIDAD_ALUMNOS_INSCRIPTOS_ASIGNATURA;
DROP TEMPORARY TABLE MATERIAS_PERMITIDAS;
END $$
DELIMITER ;