title: from each partition, select rows with N distinct values
body: <p>I have a table with three columns <code>x, y, z</code>.  I'd like to write a query that, within each <code>PARTITION BY x</code>, returns the rows containing the first <code>n</code> distinct values of <code>y</code>.</p>
  
  <p>Here's a sample for <code>n = 2</code> -- the first 2 distinct values of <code>y</code> in the first partition are 1 and 2, and 4 and 5 in the second partition, so all rows with those values of <code>y</code> are included.</p>
  
  <pre><code> x   y   z   included?
  ----------------------
   1   1   1    true
   1   1   2    true
   1   2   3    true
   1   2   4    true
   1   3   5    false
   1   3   6    false
   2   4   7    true
   2   4   8    true
   2   5   9    true
   2   5  10    true
   2   6  11    false
   2   6  12    false
  </code></pre>
  
  <p>There's a <a href="https://stackoverflow.com/a/6064141/215044">related question</a> that deals with selecting <code>n</code> rows from each partition, but that doesn't deal with the distinct values part.</p>