I have been looking all around internet and it seems like there is no answer that match my case.
I am struggling with calculating the exact Lower and Upper Quartile in SQL Server. I am aware that SQL Server has a built in function that facilitates calculation of quartiles, the NTILE function. But that is not enough for my case.
Given the following table with values (note that the table containes more products and prices than in the table below):
| AveragePrice | ProductNumber | Year |
|---|---|---|
| 45.7820 | 2 | 2015 |
| 46.0142 | 2 | 2016 |
| 59.0133 | 2 | 2017 |
| 60.1707 | 2 | 2018 |
| 62.6600 | 2 | 2019 |
I am running the following query:
SELECT
AveragePrice
,NTILE(4) OVER (
PARTITION BY ProductNumber ORDER BY AveragePrice
) AS Quartile
FROM products
Which gives the following result:
| AveragePrice | Quartile |
|---|---|
| 45.7820 | 1 |
| 46.0142 | 1 |
| 59.0133 | 2 |
| 60.1707 | 3 |
| 62.6600 | 4 |
For full context the query in it's whole looks like this:
SELECT ProductNumber
,MIN(AveragePrice) Minimum
,MAX(CASE
WHEN Quartile = 1
THEN AveragePrice
END) AS Quartile_1
,
MAX(CASE
WHEN Quartile = 3
THEN AveragePrice
END) AS Quartile_3
,MAX(AveragePrice) Maximum
,COUNT(Quartile) AS 'Number of items'
FROM (
SELECT ProductNumber
,AveragePrice
,NTILE(4) OVER (
PARTITION BY ProductNumber ORDER BY ProductNumber
) AS Quartile
FROM #temp_products
) Vals
GROUP BY ProductNumber
ORDER BY ProductNumber
But when I manually calculate the quartile the first quartile should be: 45.8981 (average of the first and second row in this particular case) not 46.0142.
The third quartile should be 61.41535 (average of the third and second quartile in this particular case) not 60.1707 .
So to make it clear. This is a part of a stored procedure where multiple pricegroups is calculated and aggregated into groups containing average prices. I need to calculate the upper and lower quartiles from these average prices grouped by product number. The result set should contain the productnumber, lower quartile and upper quartile. Can someone help me or guide me in the correct direction?
Okay, inspired by this post I managed to build a query which actually calculates the exact quartiles:
The following prices: 29.4785 30.0000 33.4762 35.2917 35.8731 36.2475 37.9790 39.5846 67.4443
Gives the correct value: Q1 = 31.7381000000 and Q3 = 38.7818000000
Verified using this online tool