• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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.
0
utlonghornjulie
Asked:
utlonghornjulie
  • 3
  • 2
1 Solution
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now