title: First value in DATE minus 30 days SQL
body: <p>I have bunch of data out of which I'm showing ID, max date and it's corresponding values (user id, type, ...). Then I need to take MAX date for each ID, substract 30 days and show first date and it's corresponding values within this date period.</p>
  
  <p>Example:</p>
  
  <pre><em>ID</em><em>  Date</em><em>        Name</em>
  1   01.05.2018  AAA
  1   21.04.2018  CCC
  1   05.04.2018  BBB
  1   28.03.2018  AAA</pre>
  
  <p>expected: </p>
  
  <pre><em>ID</em><em>  max_date</em><em>    max_name</em><em> previous_date</em> previous_name 
  <em>1</em><em>   01.05.2018</em><em>  AAA</em><em>      05.04.2018</em><em> BBB</em></pre>
  
  <p>I have working solution using subselects, but as I have quite huge WHERE part, refresh takes ages.</p>
  
  <p>SUBSELECT looks like that:
  (SELECT MIN(N.name)
      FROM t1 N
      WHERE N.ID = T.ID
          AND (N.date &lt; MAX(T.date) AND N.date >= (MAX(T.date)-30))
          AND (...)) AS PreviousName</p>
  
  <p>How'd you write the select?</p>
  
  <p>I'm using TSQL</p>
  
  <p>Thanks</p>