tags:

views:

18

answers:

1

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 number 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.

+2  A: 

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
Mark Byers
That did exactly what I was looking for. Do you know if there's a trick for starting the number_of_activities at 0? Other than simply subtracting from the total number of users. Thanks.
Justin Lucas
Yes, you can LEFT JOIN the users table with the ca_activity table, and `COUNT(ca_activity.id)` in the subquery. The outer query can remain unchanged.
Mark Byers