title: Ranking SQL Server Query results
body: <p>Is there a way in SQL Server to rank rows based on the values in the <code>WHERE</code> clause?</p>
  
  <p>For instance:</p>
  
  <pre><code>SELECT *
  FROM Foo
  WHERE 
    Bar1 = true AND
    Bar2 = true OR
    Bar3 = false
  </code></pre>
  
  <p>The best result in this case would be a row where Bar1 = true, Bar2 = true and Bar3 = false. A row where Bar1 = true, Bar2 = true and Bar3 = true would score lower because Bar3 is different.</p>
  
  <p>To make it even more complicated. The WHERE clause is dynamic, I don't know exactly which columns will appear in the query. So the next query could be:</p>
  
  <pre><code>SELECT *
  FROM Foo
  WHERE 
    Bar1 = false OR
    Bar3 = true OR
    Bar4 = True
  </code></pre>
  
  <p>How can I apply ranking to those queries?</p>
  
  <p>Can this be done within the query or is it better to do this in C#?</p>