title: How to write sql for this case?
body: <p>Suppose I have a table(a relationship) like</p>
  
  <pre><code>MyTab(ID1,ID2,IsMarked, data,....)
  </code></pre>
  
  <p>the sample data maybe looks like:</p>
  
  <pre><code>1,  1, 1, ...
  1,  2, 0, ...
  1,  3, 0, ...
  
  2,  34, 1, ...
  
  3,  4, 0, ...
  
  4,  546, 0, ...
  4,  8, 0, ...
  </code></pre>
  
  <p>Only one could be marked for each <code>ID1</code>.  I want to get data marked as <code>1</code> for all Entities <code>ID1</code>. If there is no marked record, get the first one or any one of them. </p>
  
  <p>For above sample data, the result should be:</p>
  
  <pre><code>1,  1, 1, ...
  
  2,  34, 1, ...
  
  3,  4, 0, ...
  
  4,  546, 0, ...
  </code></pre>
  
  <p>Union could be a solution, but is too long and may have bad performance. 
  My idea is to sort the data by <code>ID1</code> and  <code>IsMarked</code> desc, the get the first 1 for each <code>ID1</code>, but how to write a <code>SQL</code> for this case?</p>