开发者

MySQL query aggregating users who perform an activity

I am having trouble writing a query on a mysql table of user activity that will give me a breakdown of how active our users are. The table structure is like so:

CREATE TABLE IF NOT EXISTS `ca_activity` (
  `id` bigint(20) NOT NULL auto_increment,
  `user_id` bigint(20) default NULL,
  `activity_type` varchar(50) collate utf8_unicode_ci default NULL,
  `activity_source` varchar(255) collate utf8_unicode_ci default NULL,
  `created` timestamp NULL default NULL,
  `updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2037 ;

The result I'm looking for is the number of users who performed each numb开发者_如何学Pythoner of activities, grouped by the number of activities. For example 12330 users performed 1 activity, 9032 performed 2 activities and so on. I'm completely stuck on this so any hints would be appreciated.


You can use nested GROUP BYs:

SELECT number_of_activities, COUNT(*) AS cnt
FROM
(
   SELECT COUNT(*) AS number_of_activities
   FROM ca_activity
   GROUP BY user_id
) T1
GROUP BY number_of_activities
ORDER BY number_of_activities
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜