title: Display results from sql while loop in a single table
body: <p>In my SQL-server database there is a table in which each day has many entries for each time value (in integer form e.g. 800 --> 08:00) with different amounts and a unique ID field. </p>
  
  <p>My table looks something like this:</p>
  
  <pre><code>ID      NTIME   AMOUNT
  8426628 828     531.81
  8426629 828     782.61
  8426630 829     183.41
  8426631 829     183.41
  8426632 829     832.41
  8426633 829      32.41
  8426634 830     374.41
  8426635 830      78.41
  8426636 830     628.41
  </code></pre>
  
  <p>As a result I want to get the rows with the maximum id for each distinct time value, like this:</p>
  
  <pre><code>ID      NTIME   AMOUNT
  8426629 828     782.61
  8426633 829      32.41
  8426636 830     628.41
  </code></pre>
  
  <p>I have tried the following query:</p>
  
  <pre><code>DECLARE @t int=815;
  WHILE @t&lt;=830
    BEGIN
      select ID, NTIME, AMOUNT FROM &lt;my_table&gt; WHERE 
          NTIME=@t and 
          ID = 
             (select max(ID) FROM &lt;my_table&gt; where NTIME=@t);
      Set @t = @t + 1;
    END
  </code></pre>
  
  <p>Which fetches the correct results, but each select query result is displayed in a different table (result 1-1, result 1-2 etc.). Is there a way to get the results in a single result table, preferably without creating a temporary table?</p>