title: How to get the top row from a SQL Server record set query and other constraint
body: <p>I have two SQL Server tables as below:</p>
  <pre><code>
  Event
  
  +------------+----------------------------+-------------+------------+-----------------------------+
  | Id         | EventTypeId  | PersonId    | UCNumber    | Name       |DateEvent        
  +------------+----------------------------+-------------+------------+-----------------------------+
  | 2307       |      3       |      2189   |   004947    | Migrated   | 1900-01-01 00:00:00.6780000 |
  | 2308       |      15      |      2189   |   004947    | Birthday   | 2020-09-18 16:48:32.6870000 |
  | 3400       |      15      |      2190   |   006857    | Birthday   | 1900-01-01 00:00:00.0000000 |
  | 3401       |      2       |      2190   |   006857    | Migrated   | 2016-03-12 00:00:00.0000000 |
  
  
  Person 
  
  +------------+----------------+-------------------+-----------+-------------------------------+
  | Id         | UCNumber       |      Name         |LastName   |  AnotherDate                  | 
  +------------+----------------+-------------------+-----------+-------------------------------+
  | 2189       |      004947    |        John       |   Smith   |   1900-01-01 00:00:00.0000000 |
  | 2190       |      006857    |        Alice      |   Timo    |   2020-02-20 00:00:00.0000000 |
  
  </code></pre>
  <p>I need to get retrieved the top row (latest in time) based on the Event's <code>Id</code>. (The higher the <code>Id</code>, the more recent the Event) and it should be a <code>15</code> as <code>EventTypeId</code>.</p>
  <p>I tried this:</p>
  <pre><code>Select P.Id, P.UCNUMBER, P.AnotherDate from 
  db.dbo.Person P
  Inner join db.dbo.Event L on L.PersonId = P.Id
  where P.Id in (
      SELECT TOP (1) PersonId
      FROM
          db.dbo.Event 
      where PersonId = P.Id --and EventTypeID = 15
      ORDER BY 
          Id DESC) 
  and EventTypeId = 15
  
  </code></pre>
  <p>but it does not work properly. I posted here just samples from the 2 tables. Generally the query takes also other events which are not latest ones (as higher Id). Something is missing in it.</p>
  <p>In this case, for instance, it should return only 1 row:</p>
  <p><code>2189     004947     1900-01-01 00:00:00.0000000</code></p>