Google Answers Logo
View Question
 
Q: SQL Custom Stored Procedure: Percent Rank ( No Answer,   5 Comments )
Question  
Subject: SQL Custom Stored Procedure: Percent Rank
Category: Computers > Programming
Asked by: bloomington-ga
List Price: $5.00
Posted: 06 Aug 2002 08:36 PDT
Expires: 05 Sep 2002 08:36 PDT
Question ID: 51248
I am looking for a stored procedure to calculate percent rank
in a MS SQL 7 database (not olap).  I have a table with three columns:
ID int, SCORE decimal(11,0), and RANK decimal(11,2).  I want a
stored procedure that will run on every row in the table, read the
score and write the percent rank into RANK.

Request for Question Clarification by joseleon-ga on 06 Aug 2002 09:40 PDT
Do you want to store in the rank column the percent rank in this way?

ID     Score    Rank
1      20       10%
2      10       5% 
3      60       30%
4      80       40%
5      30       15%

That is, sum all the scores and calculate the percent of the total?

Regards.

Clarification of Question by bloomington-ga on 06 Aug 2002 11:30 PDT
I want the rank stored in the rank column, but my calculation for
percentile rank does not come up with the figures you show.  In your
example, 80 should be in the top 100% of all scores, not 40%.  I use
Excel's percentrank() function to calculate the percentile rank, but I
havent figured out what formula they use(that's something I'm hoping
to get from this :).  Take into account that there may be multiple
identical values.  I have included an example from Excel, plus your
table with the correct values from Excel.

Your Example:

ID      Score   Your Rank   Excel's Rank
1	20	10%         25%
2	10	5%          0%
3	60	30%         75%
4	80	40%         100%
5	30	15%         50%

Another Example w/ multiple identical values:

ID      Score   Rank
1	200	18%
2	500	45%
3	500	45%
4	600	64%
5	600	64%
6	700	82%
7	700	82%
8	300	27%
9	400	36%
10	100	9%
11	20	0%
12	990	100%

Thanks!

Clarification of Question by bloomington-ga on 06 Aug 2002 11:32 PDT
<pre>
I should have put a pre tag, sorry.

I want the rank stored in the rank column, but my calculation for
percentile rank does not come up with the figures you show.  In your
example, 80 should be in the top 100% of all scores, not 40%.  I use
Excel's percentrank() function to calculate the percentile rank, but I
havent figured out what formula they use(that's something I'm hoping
to get from this :).  Take into account that there may be multiple
identical values.  I have included an example from Excel, plus your
table with the correct values from Excel.

Your Example:

ID      Score   Your Rank   Excel's Rank
1	20	10%         25%
2	10	5%          0%
3	60	30%         75%
4	80	40%         100%
5	30	15%         50%

Another Example w/ multiple identical values:

ID      Score   Rank
1	200	18%
2	500	45%
3	500	45%
4	600	64%
5	600	64%
6	700	82%
7	700	82%
8	300	27%
9	400	36%
10	100	9%
11	20	0%
12	990	100%

Thanks!
</pre>

Clarification of Question by bloomington-ga on 06 Aug 2002 11:34 PDT
How do I put a pre tag in?

Clarification of Question by bloomington-ga on 06 Aug 2002 13:07 PDT
Yeah, I found that formula at
http://cyberlearn.fau.edu/dwalker/practice_percentile_ranks.htm but
the numbers dont match exactly to what excel says.  The reason, I
believe, is that Excel decides how many discreet groups to split the
data into.  I dont know how it does this.  (I act like Excel is the
end-all be-all, huh?) But I think I've decided to just make 5 ranges
divided at 20% (like a 5-star rating system)  Thanks for looking!

Clarification of Question by bloomington-ga on 06 Aug 2002 13:08 PDT
And I cant use the simpler formula, since a LARGE portion of my data
is identical (bell-curve is in effect!)  Thanks again!
Answer  
There is no answer at this time.

Comments  
Subject: Re: SQL Custom Stored Procedure: Percent Rank
From: wengland-ga on 06 Aug 2002 12:10 PDT
 
I think if you use discrete spaces it'll work - tabs get collapsed.

Lets see:
(bogus data)

ID     Value     Percent
--     -----     -------
 1       23        87
 2       44       100
 3       65        89
 4      101         9
Subject: Re: SQL Custom Stored Procedure: Percent Rank
From: wengland-ga on 06 Aug 2002 12:14 PDT
 
Yup - lots of spaces, no tabs work.  Tabs get sqooshed by whatever
backend parser GA uses.

Now to the question:  How to calculate percentile rank?

http://cyberlearn.fau.edu/dwalker/practice_percentile_ranks.htm

ASCII represntaiton of the formula:

Sb + 0.5(Sat)
-------------
     N

where 
Sb = number of students below score
.5(Sat) = half of the students at that score
N = total number of students



Another explanation of the formula:

http://ac.marywood.edu/crawley/www/stat/lectures/relative.html
Subject: Re: SQL Custom Stored Procedure: Percent Rank
From: wengland-ga on 06 Aug 2002 12:16 PDT
 
Another, simpler formula that doesn't take into account duplicate scores:

%-tile Rank of x = [(# values less than x)/n] · 100%

From :
http://www.hsru.org/csuh/review_exam1_1000sp2002.PDF
Subject: Re: SQL Custom Stored Procedure: Percent Rank
From: wengland-ga on 06 Aug 2002 12:20 PDT
 
If we can't get it (and I sure can't), you may try a posting to
comp.databases.ms-sqlserver via google at:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.databases.ms-sqlserver
Subject: Re: SQL Custom Stored Procedure: Percent Rank
From: danw2002-ga on 21 Aug 2002 18:20 PDT
 
Hello.

I wrote this SQL thinking I was a candidate for the 5 bucks.
However, after reading further, I'm not a qualified Google
researcher.  Since I did the work, I thought I'd post
it anyway, so you get to keep your money. : )

If you have any questions, you can email me at danwX@Xcox.net

remove the Xs



-- this has been formatted flush left because of
-- the formatting issues with the message board.


-- use this to drop the table
drop table #sample

-- create a table to hold data in lieu of
-- actual data table
create table #sample
(score int
,number_of_scores decimal(18,2))

-- load sample data from last example on 
-- http://cyberlearn.fau.edu/dwalker/practice_percentile_ranks.htm

insert #sample (score, number_of_scores) select 33, 10
insert #sample (score, number_of_scores) select 32, 5
insert #sample (score, number_of_scores) select 30, 6
insert #sample (score, number_of_scores) select 27, 2
insert #sample (score, number_of_scores) select 26, 4
insert #sample (score, number_of_scores) select 25, 5
insert #sample (score, number_of_scores) select 24, 9
insert #sample (score, number_of_scores) select 22, 2
insert #sample (score, number_of_scores) select 20, 3


-- Acronyms SB, SAT, and N reference
-- http://cyberlearn.fau.edu/dwalker/practice_percentile_ranks.htm


-- 1. create a table to hold the number of students below score
-- SB = the number of students below a particular score 

create table #students_below
(score int
,SB decimal(18,2))

insert #students_below
(score 
,SB)
select 		
t1.score
,isnull(sum(t2.number_of_scores), 0) as SB
from #sample t1
left join #sample t2
on t1.score > t2.score
group by t1.score


-- 2. create a table to hold the value of half of the students at that score
-- .5(SAT) = half of the students at the particular score
-- use CAST or result will be integer and not give you .5

create table #half_count
(score int
,SAT decimal(18,2))

insert #half_count
select 	
t1.score
,CAST(t1.number_of_scores as decimal (5,2))/2 as SAT
from #sample t1


-- 3. create table to hold total number of students
-- N = the number of students 
-- use variable to hold value

create table #total_students
(N int)

insert 	#total_students
(n)
select sum(number_of_scores)
from #sample



-- return values
select 
t1.score
,SB
,SAT
,N
,(SB + SAT) / N as percentile_rank
from 
#students_below t1
inner join #half_count t2
on t1.score = t2.score
cross join #total_students t3



drop table #sample
drop table #students_below
drop table #half_count
drop table #total_students

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy