title: SQL: Select all rows that meet a condition if that condition is met, but only a certain nuber rows if it isn't
body: <p>I have a requirement for an SQL Server 2005 query I need to write and it's stumping me how to accomplish it.
  I'll simplifiy it quite a bit, but the essence is that, if a client has no billings more recent than a certain date, I need to select up to 3 of that client's most recent billings. But if they have billings after that cutoff date, just display any of those billings.</p>
  
  <p>So, if my cutoff date is Jan 1, 2010 and the data I have is as follows:</p>
  
  <pre><code>ClaimID ClientID    BillingDate
  1           1          March 12, 2010
  2           1          June 3, 2010
  3           1          January 5, 2008
  4           1          February 9, 2011
  5           1          May 19, 2005
  6           2          November 20, 2005
  7           2          October 5, 2009
  8           3          January 4, 1999
  9           3          July 8, 1997
  10         3          May 7, 2010
  11         3          August 6, 1999
  12         4          May 25, 2000
  13         4          April 1, 2005
  14         4          March 9, 2009
  15         4          December 5, 2007
  16         4          December 19, 1998
  17         4          June 3, 2006
  </code></pre>
  
  <p>Then I want to select:</p>
  
  <pre><code>ClaimID ClientID    BillingDate
  1           1          March 12, 2010
  2           1          June 3, 2010
  4           1          February 9, 2011
  6           2          November 20, 2005
  7           2          October 5, 2009
  10         3          May 7, 2010
  14         4          March 9, 2009
  15         4          December 5, 2007
  17         4          June 3, 2006
  </code></pre>
  
  <p>Anyone have any ideas?
  Thanks</p>