Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate 90th Percentile, SD, Mean for data in SQL

Tags:

Hi I have a table facility. Which holds a score for each day (Multiple scores can be reported each day and both would be valid)

I need to calculate the 90th percentile, SD, and Mean for score by month.

Facility:

Id   Month Date  score
1    Jan     1    5
1    Jan     1    5
1    Jan     2    3
1    Jan     3    4
1    Jan     4    4
1    Jan     5    4
1    Feb     1    5
1    Feb     1    5
1    Feb     2    3
1    Feb     3    4
1    Feb     4    4
1    Feb     5    4

Is there any way?

Thanks for your help.

like image 964
Agga Avatar asked Nov 17 '15 19:11

Agga


People also ask

How do you find the 90th percentile using the mean and standard deviation?

To compute the 90th percentile, we use the formula X=μ + Zσ, and we will use the standard normal distribution table, except that we will work in the opposite direction.

How do you calculate 95th percentile in SQL?

Ntile is where the data is divided into that "tile" where we can think of the tile having a size, and all those sizes being the same for each tile. For your 95th percentile, you want the place where the data is divided for the 95th time. That would be the START of the 95th percentile or the MIN, not the MAX.

How does SQL Server calculate percentiles?

PERCENT_RANK() The PERCENT_RANK function in SQL Server calculates the relative rank SQL Percentile of each row. It always returns values greater than 0, and the highest value is 1. It does not count any NULL values.


1 Answers

You can use the new suite of analytic functions introduced in SQL Server 2012:

SELECT DISTINCT
            [Month],
            Mean   = AVG(Score) OVER (PARTITION BY [Month]),
            StdDev = STDEV(Score) OVER (PARTITION BY [Month]),
            P90    = PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY Score) OVER (PARTITION BY [Month])
FROM        my_table

There are 2 percentile functions: PERCENTILE_CONT for continuous distribution and PERCENTILE_DISC for discrete distribution. Picks one that suits your needs.

like image 188
Code Different Avatar answered Oct 01 '22 06:10

Code Different