title: remove outliers by group in sql
body: <p>In my column in SQL Server, I must delete outliers for each group separately. Here are my columns</p>
  
  <pre><code>select
      customer, 
      sku,
      stuff, 
      action,
      acnumber,
      year
  from
      mytable
  </code></pre>
  
  <p>Sample data:</p>
  
  <pre><code>     customer sku year stuff action
  -----------------------------------
  1         1    2 2017    10      0
  2         1    2 2017    20      1
  3         1    3 2017    30      0
  4         1    3 2017    40      1
  5         2    4 2017    50      0
  6         2    4 2017    60      1
  7         2    5 2017    70      0
  8         2    5 2017    80      1
  9         1    2 2018    10      0
  10        1    2 2018    20      1
  11        1    3 2018    30      0
  12        1    3 2018    40      1
  13        2    4 2018    50      0
  14        2    4 2018    60      1
  15        2    5 2018    70      0
  16        2    5 2018    80      1
  </code></pre>
  
  <p>I must delete outlier from stuff variable, but separately by group <code>customer+sku+year</code>.</p>
  
  <p>All that is below the 25th percentile and above 75 percentile should be considered an outlier and this principle must be respected for each group.</p>
  
  <p>How to clear dataset for next working ?</p>
  
  <p>Note, in this dataset, there is variable action (it tales value 0 and 1). It is not group variable, but outliers must be delete only for <code>ZERO(0)</code> categories of action variable.</p>
  
  <p>in R language this is decided as</p>
  
  <pre><code>remove_outliers &lt;- function(x, na.rm = TRUE, ...) {
    qnt &lt;- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
    H &lt;- 1.5 * IQR(x, na.rm = na.rm)
    y &lt;- x
    y[x &lt; (qnt[1] - H)] &lt;- NA
    y[x &gt; (qnt[2] + H)] &lt;- NA
    y
  }
  
  new &lt;- remove_outliers(vpg$stuff)
  vpg=cbind(new,vpg)
  </code></pre>