I have a table containing the logging of a web app which tracks when people log in. An example of my table is:
| user_id | date_time |
+---------+------------------+
| 0033 | 2012-11-22 10:33 | <- first login of 0033 on 2012-11-22
| 0034 | 2012-11-22 10:38 | <- first login of 0034 on 2012-11-22
| 0052 | 2012-11-22 10:43 | <- first login of 0052 on 2012-11-22
| 0052 | 2012-11-23 09:23 |
| 0066 | 2012-11-23 15:58 | <- first login of 0066 on 2012-11-23
| 0033 | 2012-11-23 16:14 |
The thing I want is a table with the amount of people that logged in for the first time on each date, i.e.:
| count | date |
+-------+------------+
| 3 | 2012-11-22 | <- there were 3 users that logged in for the first time on 2012-11-22
| 1 | 2012-11-23 |
I know I can get the date only, by doing
SELECT DATE(`date_time`) AS `date`
FROM `logging`
GROUP BY `date`
ORDER BY `date` ASC
I would like to get the second table in one query, I know it’s possible, I just don’t know how. Thanks in advance
You can use an uncorrelated subquery to get the first login date for every user and then group those dates together to get the number of first logins per day.
Demo