2 Votes Vote

Using MYSQL conditional statements and variables within a query

Posted by topdog 554 days ago Questions| count mysql query All

Hi just cant seem to construct the MYSQL query Im after.

Say I have a result of two columns: 1) browser name and 2) browser count.

Where it gets complicated is I want once 90% of the total count has been reached to rename all other browsers as others and mark the left over percentage accordingly.

I know I can get the total count as a variable before I begin the main statement:

SELECT @total := COUNT(id) FROM browser_table WHERE start LIKE "2010%";

Then I can group the results by browser:

SELECT browser, COUNT(id) AS visits
FROM browser_table
WHERE start LIKE "2010%"
GROUP BY browser

I know I need to whack in a case statement (and a counter variable) to sort the columns but not sure of how to implement the into the above query:

CASE 
  WHEN counter >= 0.9* @total THEN 'other'
  ELSE browser
END AS browser;

Hope that makes sense? Thanks for your time....

Originally asked by: Globalz on Stack Overflow

Discuss Bury


Who Voted for this Question