mysql - Skip positions on ties in RANK function -


i skip positions on ties

create table persons ( name varchar(50), totalrate int, nrrates int );   insert persons (name, totalrate, nrrates) values          ('albert', 80, 8),          ('ben', 65, 7),          ('brittany', 65, 7),          ('carl', 64, 7),          ('danna', 62, 7);  select   `name`, (`totalrate` / `nrrates`),          @rank_count := @rank_count + (totalrate/nrrates < @prev_value) rank,          @prev_value := totalrate/nrrates avg     persons, (select @prev_value := null, @rank_count := 1) init order avg desc 

i

name    (`totalrate` / `nrrates`)   rank    avg  albert                10             1     10 ben               9.2857             2   9.285714285 brittany          9.2857             2   9.285714285 carl              9.1429             3   9.142857142 danna             8.8571             4   8.857142857 

i want skip third position , have fourth place not ranked third

name    (`totalrate` / `nrrates`)   rank    avg  albert          10                    1      10 ben         9.2857                    2     9.285714285 brittany    9.2857                    2     9.285714285 carl        9.1429                    4     9.142857142 danna       8.8571                    5     8.857142857 

i hope there enough information create table , run function on sql ruins in xammp.

you need introduce variable:

select   `name`, (`totalrate` / `nrrates`) avg,          (@rank_count := if((@rn := (@rn + 1)) > 0,                             if((totalrate/nrrates < @prev_value),                                @rn,                                @rank_count), null)          ) rank,          @prev_value := totalrate/nrrates avg     persons cross join          (select @prev_value := null, @rank_count := 1, @rn := 0) init order avg desc; 

note put assignment of @rn same expression @rank_count. mysql doesn't guarantee order of evaluation of expressions in select. if done in wrong order code not expect.

i didn't change @prev_value because working , rewriting code change query looks -- hiding simple change of adding @rn.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -