开发者

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

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 little review, I would appreciate it.

I collect data in the millions, and need the top 50 grouped by one field, with a percentage of how much those top 50 occupy.

Here is what I have come up with... 1) I have a feeling I can be more efficient, perhaps with a join 2) How can I get the percentage to be of precision in the hundredths, so * 100.00 ie: .07 becomes 7.00, getting SQL errors if I (percentage * 100)

SELECT user_agent开发者_Python百科_parsed, user_agent_original, COUNT( user_agent_parsed ) AS thecount, 
    COUNT( * ) / ( SELECT COUNT( * ) FROM agents ) AS percentage
FROM agents
GROUP BY user_agent_parsed
ORDER BY thecount DESC LIMIT 50;

Second issue, once a day I need to archive the result of the above. Any suggestions on how to best to do that? I can schedule with cron, or in my case, launchd, unless someone has a better suggestion.

Would you think that a simple 'SELECT (the above) INTO foo' would suffice?


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;


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜