title: SQL Server: SELECT from multiple lines if values exist
body: <p>I am trying to write a query that would select data from multiple lines depending if there is a newer value. This has been giving me headaches for a long time already. The table is structured this way:</p>
  
  <pre><code>UniqID ProblemID  DateTime   Status PersonID ResponsibleID ActionID Comment  Deadline    PartID
  15589   15589    01/16 12:11   0       25         48          12      bla1   01/16 12:11  T9865
  15592   15589    01/16 12:25   1       48         48          105     bla2   null         null
  15601   15589    01/16 13:08   3       56         null        195     bla3   null         N2654
  15641   15589    01/17 18:02   3       11         23          35      null   01/18 15:00  null
  15705   15589    01/18 10:24   5       23         null        255     bla4   null         null
  </code></pre>
  
  <p>This is a mistake log and I need to perform several slightly different queries on this data. </p>
  
  <ul>
  <li>e.g. if person 48 was involved with this mistake (15589) as <code>PersonID</code> or as <code>ResponsibleID</code>. </li>
  </ul>
  
  <p>This query would have to return </p>
  
  <pre><code>    ProblemID (first)DateTime (last)Status (first)Person (last)Responsible (last)action (first)Comment (last)deadline (last)PartID        
        15589      01/16 12:11       5            25            23                255         bla1        01/18 15:00      N2654
  </code></pre>
  
  <ul>
  <li>e.g show all mistakes for the last 24hours</li>
  <li>e.g show all mistakes where <code>PartID</code> T9865 was involved</li>
  </ul>
  
  <p>I have made several attempts at this query, but the best that came out was a query that selects all stuff from the last line or the first one. I am actually struggling to figure out how to select the (last)<code>ResponsibleID</code> if it is not contained in the last line. The same for other columns obviously. The only solution I can think of right now is a simple select that would return me all the rows and just filter that necessary data in a foreach loop with PHP. Because selecting different values in multiple lines is just beyond my skill :)</p>
  
  <p>Thanks in advance</p>