Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server outer join issue
    primarykey
    data
    text
    <p>I'm having a bit of trouble getting an outer join to work: I've had them work as I expected in MS Access in the past, but getting a similar thing happening in SQL Server is giving me issues.</p> <p>I have a table of scores that apply to each student like:</p> <pre><code>+-------------+------------+-------+ | StudentID | StandardID | Score | +-------------+------------+-------+ | 100 | 1011 | 1 | | 100 | 1012 | 2 | | 101 | 1011 | 3 | </code></pre> <p>Each student may have many scores, and each score is related to one Standard. Additionally, each student may belong to one or more groups, which are contained within another table, groups:</p> <pre><code>+-------------+------------+ | StudentID | GroupID | +-------------+------------+ | 100 | 83 | | 101 | 83 | </code></pre> <p>What I want to do is extract the score information and filter it by group: this dataset will then be matched up by StudentID to the correct record elsewhere. However, for each retrieved dataset for any given student, there needs to be exactly the same number of rows: one for each standard. Ideally this (for the above data):</p> <pre><code>StudentID = 100 +------------+-------------+------------+-------+ | StandardID | StudentID | GroupID | Score | +------------+-------------+------------+-------+ | 1011 | 100 | 83 | 1 | | 1012 | 100 | 83 | 2 | StudentID = 101 +------------+-------------+------------+-------+ | StandardID | StudentID | GroupID | Score | +------------+-------------+------------+-------+ | 1011 | 101 | 83 | 3 | | 1012 | 101 | 83 | NULL | &lt;--Can't get this to happen </code></pre> <p>I can pull up the list that I want but there are not NULL rows in there. As a further example, if I have 4 scores for one student but only 1 score for another, I still need there to be 4 rows returned by the query, with NULLs in for the scores they don't have. </p> <p>This is what I have tried so far (a bit more verbose, but in essence):</p> <pre><code>SELECT Standards.StandardID, scores.StudentID, scores.TestDate, scores.Score, scores.Assessment FROM scores RIGHT OUTER JOIN (SELECT scores_1.StandardID FROM scores AS scores_1 INNER JOIN studentGroups ON scores_1.StudentID = studentGroups.StudentID WHERE (studentGroups.GroupID = 83) GROUP BY scores_1.StandardID) AS Standards ON scores.StandardID = Standards.StandardID WHERE scores.StudentID = 100 </code></pre> <p>Any help would be amazing!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload