December 29, 2014 at 10:35 am
Hi All,
I am using sql server 2008 to calculate the percentile value for each column.Any help on this is appreciated.
--Script
CREATE TABLE #Table (Score Varchar(4),Percentile int)
INSERT INTO #Table (Score)
VALUES
('80'),('55'),('125'),('99'),('75'),('130'),('37'),('73'),('151')
select * from #Table
I would like the result to be shown in 25,50,75,100 percentile values
Here is the example
Score | percentile
80 | 75
37 | 25
151 | 100
75 | 50
Thanks,
Venu
December 29, 2014 at 11:14 am
How about the following:
DECLARE @Table AS TABLE (Score Varchar(4), Percentile int);
INSERT INTO @Table (Score)
VALUES ('80'),('55'),('125'),('99'),('75'),('130'),('37'),('73'),('151');
WITH RANKS AS (
SELECT *, NTILE(4) OVER (ORDER BY CAST(Score AS int)) AS QUARTILE
FROM @Table
),
ASSIGNS AS (
SELECT 1 AS RN, 25 AS Percentile UNION ALL
SELECT 2, 50 UNION ALL
SELECT 3, 75 UNION ALL
SELECT 4, 100
),
GROUPED AS (
SELECT MIN(CAST(R.Score AS int)) AS Score, R.QUARTILE
FROM RANKS AS R
GROUP BY R.QUARTILE
)
SELECT G.Score, A.Percentile
FROM GROUPED AS G
INNER JOIN ASSIGNS AS A
ON G.QUARTILE = A.RN
ORDER BY G.QUARTILE;
It uses the NTILE function to distribute the rows into 1 of 4 "tiles". Then it takes the minimum score associated with each tile. Is that what you are looking for?
EDIT: I just realized the output doesn't match your desired results, so I fixed a problem with the query, but still don't get the exact same results, so you'll need to determine exactly what your criteria need to be...
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
December 29, 2014 at 12:12 pm
Thanks for the help,
Is there a way that we can assign this percentile values(25,50,75,100) to each field in the table in the same query?
December 29, 2014 at 1:11 pm
Try the following:
DECLARE @Table AS TABLE (Score Varchar(4), Percentile int);
INSERT INTO @Table (Score)
VALUES ('80'),('55'),('125'),('99'),('75'),('130'),('37'),('73'),('151');
WITH RANKS AS (
SELECT *, NTILE(4) OVER (ORDER BY CAST(Score AS int)) AS QUARTILE
FROM @Table
),
ASSIGNS AS (
SELECT 1 AS RN, 25 AS Percentile UNION ALL
SELECT 2, 50 UNION ALL
SELECT 3, 75 UNION ALL
SELECT 4, 100
)
SELECT CAST(R.Score AS int) AS Score, A.Percentile
FROM RANKS AS R
INNER JOIN ASSIGNS AS A
ON R.QUARTILE = A.RN
ORDER BY R.QUARTILE;
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
December 29, 2014 at 1:48 pm
With this query the result seems not desirable,it is dividing the total records by 4 quartiles blindly.
But my required answer should be in the Ascending order of the records in 4 quartiles.
Example Record set (1,9,3,7,4,8,10,5,2,6,11,12)
The Answer i am getting with this query is (1,9,3)---25 percentile
(7,4,8)---50 percentile etc...
Desired Answer :
(1,2,3)---25 percentile
(4,5,6) ---50 percentile
(7,8,9)---75 percentile
(10,11,12) ---100 percentile
Thanks,
Venu
December 29, 2014 at 1:55 pm
Did you have a typo in your desired results? You have the same percentile in the last two sets of numbers as you have for the first two. Also, if you want to process the records in order, then you need a field that can provide that order. If I can't sort by the value of the Score, then I need an alternative, or none of this query can work. Is there either a date/time inserted or other similar field that reflects the desired order? That field would need to be substituted in places where I have CAST the Score field AS int. Let me know...
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
December 29, 2014 at 1:57 pm
So change your ORDER BY clause to suit what order you're after.
ORDER BY r.QUARTILE, CAST(R.Score AS int);
December 29, 2014 at 1:59 pm
Also, instead of NTILE(4), the ROW_NUMBER() function would need to be used, with the OVER clause including the new sorting field I mentioned previously.
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
December 29, 2014 at 2:08 pm
yes it was a typo..
December 29, 2014 at 2:26 pm
The results from the final query I supplied are as follows:
Score Percentile
===== ========
37 25
55 25
73 25
75 50
80 50
99 75
125 75
130 100
151 100
Is this in some way incorrect ? Please be sure you have the query correct, as I recall making an update on at least one of them. Please let me know if the above results are not what you are looking for, given the supplied data.
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy