title: SQL Select where value = 'X' or min value
body: <p>I have a large customer database where the customer table has people and their relationship to an organisation.  People may belong to multiple organisations (<em>this allows divisions within organisations</em>). People with multiple organisations must have a default organisation, this is usually determined where <code>isDefault = T</code>, however the front end application can also figure the default application where <code>isDefault = F</code> by selecting the <code>MIN(RowID)</code>.</p>
  
  <p>So in the table below we know <code>PersonId</code> <code>3</code> has a default <code>OrgID</code> of <code>11</code> (IsDefault = T)
  However, I need to figure out the query for finding PersonID 12's default.
  ie </p>
  
  <pre><code>Select orgId as default from myTable
  where personID = 12
  and isDefault = 'T'
  </code></pre>
  
  <p>If 0 rows returned then perform a query like this:</p>
  
  <pre><code>Select orgId as default from myTable
  where personID = 12
  and 
  RowId in (select Min(rowId) 
  from myTable 
  where PersonId = 12)
  
  
  RowID | PersonID  | OrgId  |  isDefault
  1     |    12     |   14   |     F
  2     |    12     |   17   |     F
  3     |    3      |   11   |     T
  4     |    3      |   14   |     F
  </code></pre>