title: Return min date and corresponding amount to that distinct ID
body: <p>Afternoon</p>
  
  <p>I am trying to return the min value/ max values in SQL Server 2005 when I have multiple dates that are the same but the values in the Owed column are all different. I've already filtered the table down by my select statement into a temp table for a different query, when I've then tried to mirror I have all the duplicated dates that you can see below.</p>
  
  <p>I now have a table that looks like:</p>
  
  <pre><code>ID| Date   |Owes
  -----------------
  1  20110901 89
  1  20110901 179
  1  20110901 101
  1  20110901 197 
  1  20110901 510
  2  20111001 10
  2  20111001 211
  2  20111001 214 
  2  20111001 669
  </code></pre>
  
  <p>My current query:</p>
  
  <pre><code>Drop Table #Temp
  
  Select Distinct Convert(Varchar(8), DateAdd(dd, Datediff(DD,0,DateDue),0),112)as Date
  ,ID
  ,Paid
  Into #Temp
  From Table
  Where Paid &lt;&gt; '0'
  
  Select ,Id           
         ,Date
         ,Max(Owed)
         ,Min(Owed)          
  
  From #Temp
  Group by ID, Date, Paid
  Order By ID, Date, Paid
  </code></pre>
  
  <p>This doesn't strip out any of my dates that are the same, I'm new to SQL but I'm presuming its because my <code>owed</code> column has different values. I basically want to be able to pull back the first record as this will always be my minimum paid and my last record will always be my maximum owed to work out my total owed by ID.</p>
  
  <p>I'm new to SQL so would like to understand what I've done wrong for my future knowledge of structuring queries?</p>
  
  <p>Many Thanks</p>