title: SQL Server optional joins
body: <p>I have several tables (to be exact, 7) tables I cross join in one another. This part gives me some problems;</p>
  
  <p><strong>Table "Actions"</strong></p>
  
  <pre><code> -----------------------------------------
  | ID | Package ID | Action Type | Message |
   -----------------------------------------
  | 40 | 100340     | 0           | OK      |
  | 41 | 100340     | 12          | Error   |
  | 42 | 100340     | 2           | OK      |
  
  | 43 | 100341     | 4           | OK      |
  | 44 | 100341     | 0           | Error   |
  | 45 | 100341     | 12          | OK      |
   -----------------------------------------
  </code></pre>
  
  <p><strong>Table "Packages"</strong></p>
  
  <pre><code> ----------------------
  | ID     | Name        |
   ----------------------
  | 100340 | Testpackage |
  | 100341 | Package xy  |
   ----------------------
  </code></pre>
  
  <p>I accomplished cross joingin thm, but when there is no <code>Package</code> with an ID specified in <code>Actions</code>, all actions on that package are completely missing, rather than just leavin <code>Name</code> blank - which is what I'm trying to get.</p>
  
  <p>So, if a reference is missing, just leave the corresponding joined column blank or as an empty string...:</p>
  
  <pre><code> ----------------------------------------------------------------------
  | Package ID | Name        | Action 0 | Action 2 | Action 4 | Action 12 |
   ----------------------------------------------------------------------
  | 100340     | Testpackage | OK       | OK       |          | Error     |
  | 100341     | Package xy  | Error    |          | OK       | OK        |
   ----------------------------------------------------------------------
  </code></pre>
  
  <p>How is that possible?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Sorry, I just saw my example was completety wrong, I updated it how it should look like in the end.</p>
  
  <p>My current query looks something like this (as said above, just an extract as the actual one is about three times as long including even more tables)</p>
  
  <pre><code>SELECT
   PackageTable.ID AS PackageID,
   PackageTable.Name,
   Action0Table.Message  AS Action0,
   Action2Table.Message  AS Action2,
   Action4Table.Message  AS Action4,
   Action12Table.Message AS Action12
  FROM
   Packages AS PackageTable LEFT OUTER JOIN
    Actions AS Action0Table ON PackageTable.ID  = Action0Table.PackageID LEFT OUTER JOIN
    Actions AS Action2Table ON PackageTable.ID  = Action2Table.PackageID LEFT OUTER JOIN
    Actions AS Action4Table ON PackageTable.ID  = Action4Table.PackageID LEFT OUTER JOIN
    Actions AS Action12Table ON PackageTable.ID = Action12Table.PackageID
  WHERE
   Action0Table.ActionType  = 0 AND
   Action2Table.ActionType  = 2 AND
   Action4Table.ActionType  = 4 AND
   Action12Table.ActionType = 12
  </code></pre>