Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL Selection Problem
Message
De
03/09/1999 08:42:58
 
 
À
25/08/1999 23:16:27
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00257735
Message ID:
00261088
Vues:
19
Eva,

OK, I think I've got a decent solution - although I haven't tried it. It requires that you make two changes to your physical database model.

The first is to place a flag in the Subject table, marking a subject as mandatory or not. The second is to bread the student table into two tables, forming a more normalized schema.
Student
(stud_id)  (stud_name)

Subject
(sub_id)  (sub_name)  (sub_man)

StudentInSubject
(stud_id)  (sub_id)  (grade)
Now we can write a query that uses a cross join to create every possible combination of students and subjects, but only for the mandatory subjects. We'll use a nested subquery to fetch the grade from the StudentInSubject table.
SELECT stud_id, stud_name,
  sub_id, sub_name, 
  (SELECT grade FROM StudentInSubject
    WHERE stud_id = student.stud_id AND sub_id = subject.sub_id)
FROM student CROSS JOIN subject
WHERE subject.sub_man = 1
Then you can create another query that joins the three tables together for the non-mandatory subjects and UNION it to the above query.

-Mike
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform