title: How can I include a PERCENTILE_CONT column within a select statement without generating an error about the ORDER BY clause or aggregate function?
body: <p>I have a need to generate a particular report from some data and I am having a great deal of trouble figuring out the proper usage of PERCENTILE_CONT to provide the results I need.   I would like to include a column in my query result which shows what value is the 95th percentile from a range of values.</p>
  
  <p>I have a table as follows:</p>
  
  <pre class="lang-sql prettyprint-override"><code>customer_id sale_amount sale_date
  1   265.75  2019-09-11 00:00:04.000
  1   45.75   2019-09-10 01:00:04.000
  1   2124.77 2019-09-10 04:00:04.000
  1   66.99   2019-09-10 04:20:04.000
  1   266.49  2019-09-09 11:20:04.000
  1   3266.49 2019-09-08 11:20:04.000
  </code></pre>
  
  <p>Pretty straightforward.</p>
  
  <p>I can run the following query, no problem:</p>
  
  <pre class="lang-sql prettyprint-override"><code>select min(sale_amount) as minimum_sale, max(sale_amount) as maximum_sale, avg(sale_amount) as average_sale from sales where customer_id = 1;
  </code></pre>
  
  <p>Which results in the following output:</p>
  
  <pre class="lang-sql prettyprint-override"><code>minimum_sale    maximum_sale    average_sale
  45.75           3266.49     1006.040000
  </code></pre>
  
  <p>What I'm after is a fourth column, perc_95, which would calculate what value represents the 95th percentile in terms of sale_amount.</p>
  
  <p>This works to get me the value:</p>
  
  <pre class="lang-sql prettyprint-override"><code>select distinct customer_id, percentile_cont(0.95) WITHIN GROUP (order by sale_amount) OVER (partition by customer_id) as perc_95 from sales;
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-sql prettyprint-override"><code>customer_id perc_95
  1            2981.06
  </code></pre>
  
  <p>But I can't seem to combine them - this fails:</p>
  
  <pre class="lang-sql prettyprint-override"><code>select distinct(customer id), min(sale_amount) as minimum_sale, max(sale_amount) as maximum_sale,
   avg(sale_amount) as average_sale, percentile_cont(0.95) WITHIN GROUP (order by sale_amount) OVER (partition by customer_id) as perc_95
    from sales where customer_id = 1;
  </code></pre>
  
  <p>Output:</p>
  
  <p>Column 'sales.customer_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.</p>
  
  <p>I generally understand what this error means but I am having trouble figuring out how to deal with it in this context. </p>
  
  <p>My desired output:</p>
  
  <pre class="lang-sql prettyprint-override"><code>customer_id     minimum_sale      maximum_sale  average_sale    perc_95
  1                   45.75         3266.49  1006.040000     2981.06
  </code></pre>