0

I have this query:

SELECT Students.StudentNumber,
       Students.StudentSurname,
       Students.StudentFirstNames,
       Students.SchoolYear,
       Students.Class,
       Cycle,
       Section,
       MarksEntry.SubjectCode,
       MarksEntry.AssessmentPeriod,
       MarksEntry.SubjectMaxima,
       MarksObtained,
       Subject.SubjectName
FROM Students,
     MarksEntry,
     Subject
WHERE Students.StudentNumber = MarksEntry.StudentNumber
  AND MarksEntry.SubjectCode = Subject.SubjectCode
  AND Students.Class = MarksEntry.Class
  AND MarksEntry.SchoolYear = '2020-2021'
  AND MarksEntry.Class = '1ere LIT'
  AND MarksEntry.AssessmentPeriod = '2è P'
ORDER BY Students.StudentSurname;

I get the results this way:

enter image description here

How can I modify this query so that I can get the data listing only the students with no duplication and the score marks under each subject, with the name of the subject as column name. The subjects names are not the same, they differ from classes, so they have to be read dynamically from the subjectName column based on the query condition. Something like this:

enter image description here

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
John123
  • 17
  • 6
  • What does this question have to do with [tag:C#] and [tag:visual-studio]? – Thom A Apr 20 '21 at 10:36
  • 3
    In regards to your SQL, I **strongly** suggest you learn to format your code (or any written language); a single long line like that is impossible to read and a sure fire way to have people not entertain helping you as they have to use (waste) time making *your* code presentable. Also it's 2021, why haven't you adopted the ANSI-92 explicit JOIN syntax yet? It's been about for 29 years now. [Bad Habits to Kick : Using old-style JOINs](https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins) – Thom A Apr 20 '21 at 10:38
  • Thanks Gordon for formatting properly my question. – John123 Apr 20 '21 at 10:55
  • Yes, Gordon removing your [tag:c#] was "formatting it correctly", @John123 ... ([revisions](https://stackoverflow.com/posts/67177218/revisions)) – Thom A Apr 20 '21 at 11:55

1 Answers1

0

You can use LEFT JOIN with the table MarksEntry multiple times, where each MarksEntry corresponds to a different subject.

For example,

SELECT s.StudentNumber,
       rel.MarksObtained ReligionMarks,
       bio.MarksObtained BiologyMarks
FROM Students s
LEFT JOIN MarksEntry rel 
    ON s.StudentNumber = rel.StudentNumber 
    AND rel.SubjectName = 'Religion'
LEFT JOIN MarksEntry bio 
    ON s.StudentNumber = bio.StudentNumber 
    AND bio.SubjectName = 'Biology'

This will only work if there aren't any duplicated students in the same course, otherwise, each register will appear multiple times.

I'd also recommend you take a look at the GROUP BY clause in case there are duplicates

tonbadal
  • 1
  • 1
  • Thanks Ton Badal, for your suggestions. Is is possible to get the subjects first from the Subject table before using them in this query dynamically so I don't have to list the subject name in the query because they differ from classes to classes. Thanks – John123 Apr 20 '21 at 10:59
  • Thanks all for your suggestions about the LEFT JOIN. Thanks tonbadal for your guidance. I managed to get with LEFT JOINS and PIVOT – John123 Apr 20 '21 at 14:41