We help IT Professionals succeed at work.

Removing Outliers in a SQL Server Query

3,202 Views
Last Modified: 2014-05-23
I have a view, one column averages the last 3 months sales. Sometimes the average is skewed by large values I would like to ignore.

e.g. if I have sales of 10 for month 1, 10 for month 2, and 100 for month 3, is there any way of ignoring the large number that is skewing the average? If so does anyone know the syntax?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Harish VargheseProject Leader

Commented:
You can use a case statement in the AVG function as below:
SELECT yourcolumns, 
      AVG (CASE WHEN SALES < 100000 THEN SALES ELSE NULL END) 
FROM SALES TABLE
GROUP BY yourcolumns

Open in new window

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Well, again I live and learn. I didn't know SQL had a standard deviation function!

Thanks for the heads up!

Author

Commented:
Still not sure of syntax: -

STDEV((SalesQty1 + SalesQty2 + SalesQty3) / 3)

Returns null
CERTIFIED EXPERT

Commented:
Make sure that there are no nulls in SalesQty1, 2 or 3 and then divide by 3.0

STDEV((SalesQty1 + SalesQty2 + SalesQty3) / 3.0)
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
if any of the 3 values is null, this would be normal, and anyhow this shall be done set-wise, and not per row ...

again: what is the rule/threshold to consider 1 value to be "ignored" compared to the 2 others?
Harish VargheseProject Leader
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for the help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.