Using MYSQL conditional statements and variables within a query -


hi cant seem construct mysql query im after.

say have result of 2 columns: 1) browser name , 2) browser count.

where gets complicated want once 90% of total count has been reached rename other browsers others , mark left on percentage accordingly.

i know can total count variable before begin main statement:

select @total := count(id) browser_table start "2010%"; 

then can group results browser:

select browser, count(id) visits browser_table start "2010%" group browser 

i know need whack in case statement (and counter variable) sort columns not sure of how implement above query:

case    when counter >= 0.9* @total 'other'   else browser end browser; 

hope makes sense? time....

here's 1 approach...

you can calculate running total based on this answer. example:

set @rt := 0; select     browser,     visits,     (@rt := @rt + visits) running_total     (         select             browser,             count(id) visits                     browser_table                     start '2010%'         group             browser         order             visits desc     ) sq ; 


once have in place, can build on create 'other' category:

set @threshold := (select count(id) browser_table start '2010%') * 0.90; set @rt := 0; select     browser,     sum(visits) total_visits     (         select             if (@rt < @threshold, browser, 'other') browser,             visits,             (@rt := @rt + visits) running_total                     (                 select                     browser,                     count(id) visits                                     browser_table                                     start '2010%'                 group                     browser             ) sq1         order             visits desc     ) sq2 group     browser order     total_visits desc ; 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -