Question

Here is a sample DB extract as I don't think I was making myself clear. One change comprises multiple rows. I want to treat this as one instance.

+----+---------+---------------------+
| id | user_id | created_at          |
+----+---------+---------------------+
|  1 |       2 | 2013-10-30 12:47:04 |
|  2 |       2 | 2013-10-30 12:47:04 |
|  3 |       2 | 2013-10-30 12:47:04 |
|  4 |       2 | 2013-10-30 16:38:30 |
|  5 |       2 | 2013-10-30 16:38:30 |
|  6 |       2 | 2013-10-30 16:38:30 |
|  7 |       4 | 2013-10-30 20:49:40 |
+----+---------+---------------------+

What I want as an output is

user_id | count
2|2
4|1

as user_id 2 has two blocks of edits based on created_at and user 3 has one.

--- previous post ---

I have a table that contains records to be edited. It has a new row per change to the form, upon update it will save the time stamp of the update against each row for the change. It also saves the user_id for the changer.

I want to count the number of whole edits per user.

To do this, I would imagine I need to group by time stamp (all the same for the edit unlikely to have two come in at the same time) then count the occurrence of the user ids. I can't get it to work.

Any ideas?

No correct solution

OTHER TIPS

If you want to count number of edit by a user you should group by user id like :

select count(occurence) from tableName group by userId

This would fetch from the table for all user for entire time range.

For specific user or for a specific time period then you can mention in where clause.

If you will group by timestamp then you will get all the edits that had been made at that particular time.

If you will group by timestamp + user then it will give all the edits that have been made at that time per user wise.

You can GROUP BY with more fields this way:

GROUP BY user_id, timestamp

I think I have it. It needed a subquery.

MariaDB [dartmoor]> select user_id,count(user_id) from (select user_id, created_at from revisions group by created_at) as r group by user_id;

+---------+----------------+
| user_id | count(user_id) |
+---------+----------------+
|       2 |            199 |
|       3 |              2 |
|       4 |              1 |
+---------+----------------+
3 rows in set (0.00 sec)

I think you want:

SELECT user_id, COUNT(DISTINCT created_at) AS count
FROM revisions 
GROUP BY user_id ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top