title: LEFT JOIN ONLY FOR SPECIFIC ROWS IN SQL?
body: <p>I have another rather curious problem. I have the following structure:</p>
  
  <pre><code>CREATE TABLE [dbo].[Event]
  (
      Id int IDENTITY(1,1) NOT NULL,
      ApplicationId nvarchar(32) NOT NULL,
      Name nvarchar(128) NOT NULL,
      Description nvarchar(256) NULL,
      Date nvarchar(16) NOT NULL,
      Time nvarchar(16) NOT NULL,
      EventType nvarchar(16) NOT NULL,
      SourceId int NOT NULL,
      CONSTRAINT Event_PK PRIMARY KEY CLUSTERED ( Id ) WITH (
          PAD_INDEX = OFF, 
          STATISTICS_NORECOMPUTE = OFF, 
          IGNORE_DUP_KEY = OFF, 
          ALLOW_ROW_LOCKS = ON, 
          ALLOW_PAGE_LOCKS  = ON
      ),
      CONSTRAINT Event_Source FOREIGN KEY (SourceId) REFERENCES [dbo].[Source](Id)
  )
  
  CREATE TABLE [dbo].[Source]
  (
      Id int IDENITY(1,1) NOT NULL,
      Name nvarchar(128) NOT NULL,
      Description nvarchar(256) NULL,
      CONSTRAINT Source_PK PRIMARY KEY CLUSTERED ( Id ) WITH (
          PAD_INDEX = OFF, 
          STATISTICS_NORECOMPUTE = OFF, 
          IGNORE_DUP_KEY = OFF, 
          ALLOW_ROW_LOCKS = ON, 
          ALLOW_PAGE_LOCKS  = ON
      )
  )
  </code></pre>
  
  <p>The data from the event table has to be displayed in a grid. In my previous question I learned how to properly query and display the data while only displaying the last record per ApplicationId (aka... grouping it and displaying the last record for each group). (http://stackoverflow.com/questions/6201253/how-to-get-the-last-record-per-group-in-sql) Thank you @Anthony Faull, @Damien_The_Unbeliever, and all others who contributed to that answer.</p>
  
  <p>Unfortunatelly a new requirement has been thrown at me and it involves the SourceId and EventType fields of this table.</p>
  
  <p>Basically the new requirement states that for a specific value of in the EventType ('APP_CLOSE' and 'APP_START') column grab the Name of the source. For everything else display 'NULL' or some other predefined value.</p>
  
  <p>I am trying to create a view that will accommodate this request and attempted using a LEFT OUTER JOIN. The problem is that some records in the Event table have values in the SourceId and I don't the data brought by the JOIN for these records unless the EventType field is 'APP_CLOSE' or 'APP_START'. </p>
  
  <p>Any ideas on how to do this?</p>
  
  <p><em><strong>EDIT</em>*</strong></p>
  
  <p>Since someone pointed out that I suck at explaining what the issue is I decided to do a follow up:</p>
  
  <p>Event:</p>
  
  <pre><code>| Id | ApplicationId  | Name  | Description | Date       |Time     | EventType | SourceId |
  +----+----------------+-------+-------------+------------+---------+-----------+----------+
  |1   |2202            |XYZ    | Test        | 05/31/2011 |10:30:55 | APP_CLOSE | 2        |
  +----+----------------+-------+-------------+------------+---------+-----------+----------+
  |2   |2709            |zyx    | Test        | 05/31/2011 |11:27:55 | APP_START | 4        |
  +----+----------------+-------+-------------+------------+---------+-----------+----------+
  |3   |2709            |zyx    | Test        | 05/31/2011 |17:09:55 | APP_PAUSE | 1        |
  </code></pre>
  
  <p>Source Table:</p>
  
  <pre><code>| Id | Name              | Description |
  +----+-------------------+-------------+
  | 2  | Process Watcher   |             |
  +----+-------------------+-------------+
  | 4  | Interrupt Handler |             |
  +----+-------------------+-------------+
  | 1  | User Input        |             |
  +----+-------------------+-------------+
  </code></pre>
  
  <p>Result (Query or View):</p>
  
  <pre><code>| Id | ApplicationId  | Name  | Description | Date       |Time     | EventType | Source Name          |
  +----+----------------+-------+-------------+------------+---------+-----------+----------------------+
  |1   |2202            |XYZ    | Test        | 05/31/2011 |10:30:55 | APP_CLOSE | Process Watcher      |
  +----+----------------+-------+-------------+------------+---------+-----------+----------------------+
  |2   |2709            |zyx    | Test        | 05/31/2011 |11:27:55 | APP_START | Interrupt Handler    |
  +----+----------------+-------+-------------+------------+---------+-----------+----------------------+
  |3   |2709            |zyx    | Test        | 05/31/2011 |17:09:55 | APP_PAUSE |                      |       
  </code></pre>
  
  <p>The idea is that even though a user can pause the app, the user is not a tangible piece of the software. Therefore when reporting on Events that have occurred I can only report on events that are caused by the components of the software. In other words I only need to bring in the Source Name based on the specific event types (APP_CLOSE and APP_START) for now. </p>
  
  <p>Thanks,</p>
  
  <p>Martin</p>