title: Combining two tables and filtering only last date
body: <p>I am attempting to combine two tables.  What I am attempting to find is if a person has an active plan or not.  It is something that the person can sign up for and discontinue. </p>
  
  <p>From Table One, <code>Persons</code>, I am these columns:</p>
  
  <pre><code>PerID    FacID
   993     Fac1
  1052     Fac1
  1163     Fac2
  </code></pre>
  
  <p>From Table Two, <code>PlanDates</code>, I am these columns:</p>
  
  <pre><code>PerID   FacID   StartDt                     EndDt
   993    Fac1    2017-07-01 00:00:00.000     2017-07-31 00:00:00.000
   993    Fac1    2017-08-05 00:00:00.000     NULL
  1053    Fac1    NULL                        NULL
  1163    Fac2    2017-08-01 00:00:00.000     NULL
  </code></pre>
  
  <p>I am running this Query</p>
  
  <pre><code>Select
    A.PerID
    ,A.FacID
    ,B.PerID
    ,iif(CASE WHEN B.StartDt IS NULL THEN 'FALSE' ELSE 'TRUE' END = 'True', iif(CASE WHEN B.EndDt IS NULL THEN 'FALSE' ELSE 'TRUE' END = 'False', 'Plan A', 'None'),'None') AS [Plan Code]
    ,B.FacID
  From
    Persons A
    Left Join PlanDates B
      On A.PerID = B.PerID
      And A.FacID = B.FacID
  </code></pre>
  
  <p>The results I get are:</p>
  
  <pre><code>PerID   Plan Code
   993    None
   993    Plan A
  1052    None
  1163    Plan A
  </code></pre>
  
  <p>I am looking to not have the first result for PerID 993.  </p>
  
  <p>The reason I have that iif statement in the query is to make the dates Boolean.  </p>
  
  <p>I included the FacID because some PerIDs are at multiple locations when a person visits moves closer to another location.  </p>
  
  <p>I am running the query on SQL Server.</p>