Getting a percentage from MySql with a group by condition, and precision

前端 未结 2 1413
旧时难觅i
旧时难觅i 2020-12-11 07:35

I was about to ask the MySql list this and remembered about SO.

Running MySql 5.0.85, I need to be as efficient as possible about a few queries. If I could get a li

相关标签:
2条回答
  • 2020-12-11 08:04

    First Issue:

    select count(*) from agents into @AgentCount;
    
    SELECT user_agent_parsed
         , user_agent_original
         , COUNT( user_agent_parsed )  AS thecount
         , COUNT( * ) / ( @AgentCount) AS percentage
     FROM agents
    GROUP BY user_agent_parsed
    ORDER BY thecount DESC LIMIT 50;
    
    0 讨论(0)
  • 2020-12-11 08:16

    I quite don't understand your question fully so I'll just answer first your question on how to get the percentage. And I'll use your present query.

     SELECT user_agent_parsed, user_agent_original, COUNT( user_agent_parsed ) AS thecount, 
        ((COUNT( * ) / ( SELECT COUNT( * ) FROM agents)) * 100 ) AS percentage
    FROM agents
    GROUP BY user_agent_parsed
    ORDER BY thecount DESC LIMIT 50;
    

    In order for me to help you further, I think I need you to elaborate it further ;-)

    0 讨论(0)
提交回复
热议问题