?
Solved

HOW TO CALCULATE AVERAGE IN SQL WITH BAD DATA POINTS

Posted on 2008-11-18
6
Medium Priority
?
181 Views
Last Modified: 2012-05-05
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
Comment
Question by:utlonghornjulie
  • 3
  • 2
6 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 22985215
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
 
LVL 3

Author Comment

by:utlonghornjulie
ID: 22985246
Sorry about the caps, the criteria would be if majority is low then change status column for the larger numbers.
0
 
LVL 3

Accepted Solution

by:
utlonghornjulie earned 0 total points
ID: 22985345
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22985354
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22985424
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22985448
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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question