Avatar of HKFuey
HKFuey
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Removing Outliers in a SQL Server Query

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?
Microsoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
HKFuey

8/22/2022 - Mon
SOLUTION
Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Harish Varghese

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

ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Lee

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

Thanks for the heads up!
HKFuey

ASKER
Still not sure of syntax: -

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

Returns null
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Lee

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]

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?
SOLUTION
Harish Varghese

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HKFuey

ASKER
Thanks for the help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.