HOW TO CALCULATE AVERAGE IN SQL WITH BAD DATA POINTS

I HAVE TWO COLUMNS STATUS AND DATA, STATUS IS EITHER 0 OR 250 BASED OFF OF DATA
EXAMPLE
STATUS    DATA
0                1.2
0                .9
0                1.6
0                18976.1
0                800.25
WHAT I AM WANTING TO DO IS TO CHANGE THE STATUS OF 0 TO 250 WHEN THERE IS BAD DATA.  THE LAST TWO DATA COLUMNS ARE BAD BECAUSE THE NUMBERS ARE SO MUCH LARGER THEN THE FIRST 3.  THE TABLE I AM USING CONSISTS OF 4 MILLION ROWS AND THIS JOB WILL DO A SWEEP ON THIS TABLE ON A NIGHTLY BASIS.
LVL 3
utlonghornjulieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Hello utlonghornjulie,

Two things:

1) Take off your caps lock.  In pretty much every online forum, it is considered rude to post in all caps.

2) Please define the criteria we should use in determining that values in the DATA column are "so much larger"
and thus should get a STATUS value of 250.

Regards,

Patrick
0
utlonghornjulieAuthor Commented:
Sorry about the caps, the criteria would be if majority is low then change status column for the larger numbers.
0
utlonghornjulieAuthor Commented:
I RESOLVED THIS BUY USING A CTE AND TAKING THE AVERAGE OF THE TOP 10 FOR EACH GROUP AND CHANGE THE STATUS IF # IN DATA COLUMN IS 5X GREATER THEN THE AVERAGE OF THE TOP 10 FOR EACH GROUP.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Kevin CrossChief Technology OfficerCommented:
It would be helpful to see your criteria, but here is a general idea of how I would do something like that.

UPDATE TableName a
SET a.Status = CASE
    -- take number and divide by 100 or subtract some number
    -- compare that to the max amount from data that is less than this number that is not set to 250 status
    WHEN (a.Data / 100) > (SELECT MAX(Data) FROM TableName b WHERE b.Data < a.Data AND b.Status = 0) THEN 250
    ELSE 0 END

0
Kevin CrossChief Technology OfficerCommented:
A CTE is an excellent approach for this.

;WITH cte AS (
    SELECT Status, Data
    , row_number() OVER (ORDER BY Data) As rank
    FROM TableName
    WHERE Status = 0
)
UPDATE cte
SET Status = 250
WHERE Data > (SELECT AVG(Data) FROM cte WHERE rank <= 10)
0
Kevin CrossChief Technology OfficerCommented:
And you said, 5x -- just seeing if this is close to the approach you used to learn for myself.

;WITH cte AS (
    SELECT Status, Data
    , row_number() OVER (ORDER BY Data) As rank
    FROM TableName
    WHERE Status = 0
)
UPDATE cte
SET Status = 250
WHERE Data > (SELECT AVG(Data) * 5 FROM cte WHERE rank <= 10)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.