Вопрос

Everyone, I'm building a report using Visual Studio 2012

I want to be able to average a group of values between a specific set of rows.

What I have so far is something like this.

=(Count(Fields!SomeField.Value))*.1

and

=(Count(Fields!SomeField.Value))*.9 

I want to use those two values to get the Average of Fields!SomeField.Value between those to numbers of the row. Basically I'm removing the top and bottom 10% of the data and get the middle 80% to average out. Maybe there is a better way to do this? Thanks for any help.

Это было полезно?

Решение

Handle it in SQL itself. Method 1: Use NTILE function. Go through this link to learn more about NTILE.

Try something like this

WITH someCTE AS (
 SELECT SomeField, NTILE(10) OVER (ORDER BY SomeField) as percentile
 FROM someTable)

SELECT AVG(SomeField) as myAverage 
FROM someCTE WHERE percentile BETWEEN 2 and 9

if your dataset is bigger

WITH someCTE AS (
 SELECT SomeField, NTILE(100) OVER (ORDER BY SomeField) as percentile
 FROM someTable)

SELECT AVG(SomeField) as myAverage 
FROM someCTE WHERE percentile BETWEEN 20 and 90

METHOD 2:

SELECT Avg(SomeField) myAvg
From someTable
Where SomeField NOT IN
(
SELECT Top 10 percent someField From someTable order by someField ASC
UNION ALL
SELECT Top 10 percent someField From someTable order by someField DESC
)

Note: Test for boundary conditions to make sure you are getiing what you need. If needed tweak above sql code.

For NTILE: Make sure you NTILE parameter is less(or equal) than the number of rows in the table.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top