title: SQL -- How to select a unique field with another field associated with min value
body: <p>There is a table that has three attributes:</p>
  
  <pre><code>TableA:
       -- Attribute1
       -- Attribute2
       -- Attribute3
  </code></pre>
  
  <p>Now I want to get all three attributes out, with distinct Attribute2 that has the minimum value of Attribute3</p>
  
  <p>Here is the SQL statement that I can come up with:</p>
  
  <pre><code>select TableA.Attribute1, TableA.Attribute2, TableA.Attribute3
  from TableA, 
  (select Attribute2, min(Attribute3) as minAttribute3
  from TableA
  group by Attribute2) as TableB
  where TableA.Attribute2 = TableB.Attribute2
  and TableA.Attribute3= TableB.minAttribute3
  </code></pre>
  
  <p>Example table data is as follows:</p>
  
  <pre><code>Attribute1  Attribute2  Attribute3
  att1_001    att2_001    1
  att1_002    att2_001    2
  att1_003    att2_001    3
  att1_004    att2_002    10
  att1_005    att2_002    11
  </code></pre>
  
  <p>Here is the expected output result:</p>
  
  <pre><code>Attribute1  Attribute2  Attribute3
  att1_001    att2_001    1
  att1_004    att2_002    10
  </code></pre>
  
  <p>I am sure this is not an optimized one and am looking for one:)</p>
  
  <p>Thank you in advance</p>