title: Impala Calculate Quantile From other Quantile
body: <p>As an example suppose we have the table:</p>
  
  <pre><code>name | val
  -----------
  Alice| 1
  Bob  | 2
  </code></pre>
  
  <p>I can get the quantiles of the above values by doing</p>
  
  <pre><code>SELECT *, NTILE(2) over (ORDER BY val ASC) AS quantile FROM table
  </code></pre>
  
  <p>which gives:</p>
  
  <pre><code>name | val | quantile |
  -----------|----------|
  Alice| 1   |     1
  Bob  | 2   |     2    
  </code></pre>
  
  <p>Suppose now I have a bigger table </p>
  
  <pre><code>name | val
  -----------
  Alice| 1
  Bob  | 2
  Joe  | 1.8
  </code></pre>
  
  <p>I would like to calculate the quantile for Joe <strong>with respect to the previous quantile</strong>. In other words my previous quantile does a split of "&lt;1.5" and ">=1.5", so that I should see:</p>
  
  <pre><code>name | val | quantile |
  -----------|----------|
  Alice| 1   |    1     |
  Bob  | 2   |    2     |
  Joe  | 1.8 |    2     |
  </code></pre>
  
  <p>Is there an easy way of doing this with <strong>Impala</strong>, in a way that generalizes to NTILE(n) for arbitrary n? In theory, I could find the minimum and maximum values for each quantile level from the prior quantile (suppose for simplicity val is continuous), but this is very tedious. </p>