MySQL Conditional count based on a value in another column -


i have table looks this:

id rank  2  1 b  4 b  3 c  7 d  1 d  1 e  9 

i need distinct rank values on 1 column , count of unique id's have reached equal or higher rank in first column.

so result need this:

rank count 1    5 2    4 3    3 4    3 7    2 9    1 

i've been able make table unique id's max rank:

select  max(rank) 'toprank', id mytable group id 

i'm able distinct rank values , count how many id's have reached rank:

select distinct toprank 'rank', count(id) 'count of id'   (select    max(rank) 'toprank',   id   mytable   group id) tablederp group toprank order toprank asc 

but don't know how count of id's rank equal or higher rank in column 1. trying sum(case when toprank > toprank 1 end) naturally gives me nothing. how can count of id's toprank higher or equal each distinct rank value? or looking in wrong way , should try running totals instead? tried similar questions think i'm on wrong trail here since couldn't find , seems pretty simple problem i'm overthinking somehow. appreciated.

one approach use correlated subquery. list of ranks , use correlated subquery count looking for:

select r.rank,        (select count(distinct t2.id)         mytable t2         t2.rank >= r.rank        ) cnt (select distinct rank mytable) r; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -