title: SQL: Return first instance of an access request
body: <p>I rarely use SQL and I'm having trouble with this task.
  This table has all the information I need.</p>
  
  <p>A person can request access to a sector of a building, and for that 2 types of approbations may be needed. </p>
  
  <p>I have to discover the data about the first request a user did for each building it has access</p>
  
  <p>If user Jon requested access to sector "A" and 2 weeks later for sector "B", I just have to return the information about 
  who approved sector's "A" and when.</p>
  
  <p>I can find out the which sector was requested first and the date. But I don't know how to return the approvers since they are in another 
  row. I think the key here is to use the ticket number.</p>
  
  <p><strong>Records</strong></p>
  
  <pre><code>ID  EVENTDATE   TICKET  USER    ACTION  EVENT   APPROVER    BUILDING    SECTOR  STATUS
  15  7/1/2015 12:25  17C9F862    4003321 New access request  Started -   OHIO    IT_2    Running
  14  7/1/2015 12:41  17C9F862    4003321 Approved    Manager approval    4001719 OHIO    IT_2    Running
  12  7/1/2015 15:29  17C9F862    4003321 -   Finished    -   OHIO    IT_2    Finished: Approved
  13  7/1/2015 15:29  17C9F862    4003321 Approved    Director Approval   4003468 OHIO    IT_2    Running
  10  7/1/2015 20:57  897B9A0A    4003321 New access request  Started -   OHIO    DEVELOPMENT Running
  11  7/1/2015 20:57  F3DCFB96    4003321 New access request  Started -   OHIO    INFRA_2 Running
  9   7/1/2015 20:58  897B9A0A    4003321 Approved    Manager approval    4001719 OHIO    DEVELOPMENT Running
  8   7/1/2015 20:58  F3DCFB96    4003321 Approved    Manager approval    4001719 OHIO    INFRA_2 Running
  7   7/1/2015 21:01  F3DCFB96    4003321 Approved    Director Approval   4001547 OHIO    INFRA_2 Running
  6   7/1/2015 21:01  F3DCFB96    4003321 -   Finished    -   OHIO    INFRA_2 Finished: Approved
  4   7/1/2015 21:03  897B9A0A    4003321 -   Finished    -   OHIO    DEVELOPMENT Finished: Approved
  5   7/1/2015 21:03  897B9A0A    4003321 Approved    Director Approval   4001549 OHIO    DEVELOPMENT Running
  3   7/1/2015 21:22  3E18483E    4003321 Approval not needed Finished    -   OHIO    IT_1    Finished: Approved
  2   7/2/2015 9:48   F902EB9C    4003321 Approval not needed Finished    -   UTAH    FINANCE Finished: Approved
  1   7/2/2015 11:08  C186101C    4003321 Approval not needed Finished    -   OHIO    INFRA_1 Finished: Approved
  </code></pre>
  
  <p><strong>Desired Result:</strong></p>
  
  <pre><code>USER    Manager Approver    Director Approver   BUILDING    SECTOR  DATE
  4003321 4001719 4003468 OHIO    IT_2    7/1/2015 15:29
  4003321 -   -   UTAH    FINANCE 7/2/2015 9:48
  </code></pre>
  
  <p><strong>SQL used</strong></p>
  
  <pre><code>SELECT SEL.USER
       , SEL.BUILDING
       , SEL.SECTOR
       , SEL.EVENTDATE
       , SEL.TICKET 
  FROM ACCESSREQUESTS SEL 
  INNER JOIN 
           (
            SELECT USER
                 , BUILDING
                 , SECTOR
                 , MIN(EVENTDATE) as data 
           FROM ACCESSREQUESTS 
           WHERE EVENT = 'Finished' 
             AND STATUS = 'Finished: Approved' 
             AND USER != 'null' 
           GROUP BY USER
                  , BUILDING
                  , SECTOR 
           ORDER BY USER
          ) RE ON RE.DATA = SEL.EVENTDATE 
              AND SEL.STATUS = 'Finished: Approved'
  </code></pre>