title: Select the max and second max value from a select, and group them?
body: <p>I've been battling with this problem for a week now and I need help.</p>
  
  <p>I have a select statement that generates a table showing every product, and an import date per product.</p>
  
  <p>How can I make a new table that shows:  </p>
  
  <ul>
  <li>Product in one column.</li>
  <li>Last date in another column. </li>
  <li>Date before the last in the last column.</li>
  <li><strong>And group them by product.</strong></li>
  </ul>
  
  <p>I've scoured the net for information, and I can't get it to work. Most of the examples show how to find <strong>just one</strong> second last value from a table. <strong>I'd like to show it per product.</strong> </p>
  
  <p>Also, I can't use LIMIT due to using MSSQL.</p>
  
  <p><strong>Table from query</strong></p>
  
  <p><a href="https://i.stack.imgur.com/mdJH3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mdJH3.jpg" alt="Result of query"></a></p>
  
  <p><strong>Query</strong></p>
  
  <pre><code>SELECT
      S.acIdent AS Product,
      CAST((SELECT top 1 adDate FROM the_move WHERE ackey = MI.acKey) AS date) AS ImportDate
  FROM tHE_Stock S
      LEFT JOIN tHE_MoveItem MI
          ON S.acIdent = MI.acIdent
  WHERE
      ( S.acWarehouse = '00051' ) AND
      (
          ( RIGHT(LEFT(MI.acKey,5),3) = '100' ) OR 
          ( RIGHT(LEFT(MI.acKey,5),3) = '190' )
      ) 
  ORDER BY 
      S.acIdent, 
      CAST((SELECT top 1 adDate FROM the_move WHERE ackey = MI.acKey) AS date) 
          DESC         
  </code></pre>