{"id":546,"date":"2012-07-24T19:09:22","date_gmt":"2012-07-24T14:09:22","guid":{"rendered":"http:\/\/www.esearing.com\/v2010\/?p=546"},"modified":"2012-10-07T19:05:57","modified_gmt":"2012-10-07T14:05:57","slug":"getting-averages-by-removing-outliers","status":"publish","type":"post","link":"https:\/\/www.esearing.com\/v2010\/2012\/07\/getting-averages-by-removing-outliers\/","title":{"rendered":"Getting Averages by removing outliers"},"content":{"rendered":"<p>I had an interesting challenge this week. I needed to get average sales by product but the boss wants to eliminate the top 10% and bottom 10% so she can get a better picture of the actual sales values. We have some customers who buy very few of a product or very many of a product, but most order standard lots. So I learned about the NTILE() function in sql.<\/p>\n<p><code><br \/>\n-- create temp table of sales by a specific product.<br \/>\nselect sales<br \/>\ninto #temp0<br \/>\nfrom table_name<br \/>\nwhere product_code = 'B149001'<br \/>\norder by sales<\/p>\n<p>-- create ntiles of min and max sales by 10% quartiles. #temp1<br \/>\n-- ntile(10) can be any value like ntile(4).<br \/>\nselect  max(sales) as [max_sales], min(sales) as [min_sales], quartile<br \/>\ninto #temp1<br \/>\nfrom    (select sales, ntile(10) over (order by sales) as [quartile]<br \/>\n         from #temp0<br \/>\n) i<br \/>\ngroup by quartile<\/p>\n<p>-- get the average of the middle 80% of records by eliminating values<br \/>\n-- in the bottom 10% and top 10%.<br \/>\nselect avg(sales) as avg_sales from #temp0<br \/>\nwhere sales &gt; (select max_sales from #temp1 where quartile = 1)<br \/>\nand sales &lt; (select min_sales from #temp1 where quartile = 10)<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had an interesting challenge this week. I needed to get average sales by product but the boss wants to eliminate the top 10% and bottom 10% so she can get a better picture of the actual sales values. We &hellip; <a href=\"https:\/\/www.esearing.com\/v2010\/2012\/07\/getting-averages-by-removing-outliers\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,14],"tags":[],"class_list":["post-546","post","type-post","status-publish","format-standard","hentry","category-code","category-sql"],"_links":{"self":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/comments?post=546"}],"version-history":[{"count":3,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":588,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/546\/revisions\/588"}],"wp:attachment":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}