sql - Working out a percentage group -


i'm trying work out percentage value (e.g. 1 100) range of numbers fall into.

for example, if have simple table of 500 records:

declare @temp table (id int, percentage int)  insert @temp (id) select number master..spt_values type = 'p'  , number between 1 , 500  select * @temp 

then i'd work out first 1% of records (i.e. id 1 5) be percentage number 1, second 1% of records (i.e. id 6 10) number 2 etc.

i'm seen of more recent versions on sql server have percentage based functions percentile , percent_rank think might have been possibility, i'm using sql server 2005 not sure if options limited?

is there easy way i'm missing?

thanks!

try this, number of rows 502, first 2 percentage groups of data include "extra" row:

declare @temp table (id int, percentage int)  insert @temp (id, percentage) select    number,    ntile(100) on (order number) master..spt_values type = 'p'  , number between 1 , 500  select * @temp 

result:

id  percentage 1   1 2   1 3   1 4   1 5   1 6   2 7   2 8   2 9   2 10  2 11  3 ..  .. ..  .. 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -