Maison > Questions et réponses > le corps du texte
J'ai deux tables MySql avec le schéma suivant
Courses(CNO:CHAR(10),CName:VARCHAR(50), DeptId:CHAR(3)): Each course is given a unique ID called CNO Register(RNO:CHAR(10), CNO:CHAR(10), Year:INTEGER, Sem:INTEGER, Grade:CHAR)
Maintenant je dois trouver tous les triples (RNO, année, Sem) pour que l'étudiant ait le numéro de rôle RNO au semestre Tel que défini par année, Sem a suivi/s'est inscrit à au moins trois cours différents ou plus, chacun proposé par un département différent.
Mon code est le suivant
With tmp AS( SELECT Register.RNO,Register.CNO,Register.Year,Register.Sem,courses.DeptId, ROW_NUMBER() OVER (PARTITION BY DeptId) AS rn from Register inner join courses on courses.CNO=Register.CNO ) select RNO, Year, Sem from tmp where rn=1 GROUP BY RNO,Year,Sem having COUNT(CNO)>=3;
(J'ai utilisé Row_Number pour appliquer le mot-clé Distinct sur l'une des colonnes) Cependant, ce code échoue si l'étudiant est inscrit à plus de trois cours dans deux semestres différents et que le résultat est 1 ligne au lieu de 2 lignes. Pourquoi cela se produit-il et comment y remédier
Voici le code SQL pour les exemples de données et le schéma
CREATE TABLE courses ( CNO CHAR(10), CName VARCHAR(50), DeptId CHAR(3), PRIMARY KEY (CNO) ); INSERT INTO courses (CNO,CName,DeptId) VALUES ("CS1","database","CSE"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("CS2","deep","CSE"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("CS3","prob","CSE"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("MBA4","consume","IME"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("MBA5","Model","IME"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("MBA7","consume","IME"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("MBA8","Model","IME"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("SOC6","Social","HSS"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("MBA10","consume","IME"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("MBA11","Model","IME"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("SOC12","Social","HSS"); INSERT INTO courses (CNO,CName,DeptId) VALUES ("SOC9","Social","HSS"); CREATE TABLE Register ( RNO CHAR(10), CNO CHAR(10), Year INTEGER, Sem INTEGER, Grade CHAR, FOREIGN KEY (CNO) REFERENCES courses(CNO) ); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","CS1",1,1,"B"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","CS2",1,1,"A"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","CS3",1,1,"B"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","MBA4",1,1,"B"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","MBA5",1,1,"A"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","SOC6",1,1,"A"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","MBA7",2,2,"B"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","MBA8",2,2,"A"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","SOC9",2,2,"A"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","MBA10",3,3,"B"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","MBA11",4,3,"A"); INSERT INTO Register (RNO,CNO,Year,Sem,Grade) VALUES ("1","SOC12",5,3,"A");
Voici le lien vers DB Fiddle.
P粉9377693562024-02-26 10:50:57
Si j'ai bien deviné
select Register.RNO, Register.Year, Register.Sem from Register inner join courses on courses.CNO=Register.CNO group by RNO,Year,Sem having COUNT(distinct courses.CNO)>=3 and COUNT(distinct courses.DeptId) >=3;