Home > Software engineering >  Get count of distinct datetime range values in MySQL
Get count of distinct datetime range values in MySQL

Time:05-05

I have two tables in my MySQL database:

users_metrics
id | user_id  | login_datetime      | logout_datetime   |
--------------------------------------------------.-----|
1  |    1     |2022-05-04 00:25:16  |2022-05-04 00:30:16|
-----------------------------------------------.--------|
2  |    3     |2022-05-04 10:29:16  |2022-05-04 10:40:16|
--------------------------------------------------------|
3  |    1     |2022-05-04 11:48:16  |2022-05-04 11:49:56|
--------------------------------------------------------|
4  |    3     |2022-05-04 11:58:16  |2022-05-04 12:20:16|
--------------------------------------------------------|
5  |    1     |2022-05-04 16:28:16  |2022-05-04 17:29:56|
--------------------------------------------------------|

files_uploaded
id  | type   | file_upload_datetime | user_id |
-----------------------------------------------
1   |  csv   |2022-05-04 00:29:16   |   1     |
-----------------------------------------------
2   |  csv   |2022-05-04 10:39:16   |   3     |
-----------------------------------------------
3   |  txt   |2022-05-04 11:49:16   |   1     |
-----------------------------------------------
4   |  txt   |2022-05-04 11:59:16   |   3     |
-----------------------------------------------
5   |  csv   |2022-05-04 12:09:16   |   3     |
-----------------------------------------------
6   |  txt   |2022-05-04 12:19:16   |   3     |
-----------------------------------------------
7   |  txt   |2022-05-04 16:29:16   |   1     |
-----------------------------------------------
8   |  csv   |2022-05-04 16:39:16   |   1     |
-----------------------------------------------
9   |  txt   |2022-05-04 16:49:16   |   1     |
-----------------------------------------------
10  |  csv   |2022-05-04 17:29:16   |   1     |
-----------------------------------------------

The users_metrics table save the login and logout time of a certain user and the files_uploaded table records files records uploaded by users. I am looking for a query that allows obtaining the count of uploaded files within to the datetime range established by the first table. This would be a view of the result I'm looking for.

id | user_id  | login_datetime      | logout_datetime   | total_files_uploaded
--------------------------------------------------.-----|---------------------|
1  |    1     |2022-05-04 00:25:16  |2022-05-04 00:30:16|        1      
-----------------------------------------------.--------|---------------------|
2  |    3     |2022-05-04 10:29:16  |2022-05-04 10:40:16|        1
--------------------------------------------------------|---------------------|
3  |    1     |2022-05-04 11:48:16  |2022-05-04 11:49:56|        1
--------------------------------------------------------|---------------------|
4  |    3     |2022-05-04 11:58:16  |2022-05-04 12:20:16|        3
--------------------------------------------------------|---------------------|
5  |    1     |2022-05-04 16:28:16  |2022-05-04 17:29:56|        3
--------------------------------------------------------|---------------------|

CodePudding user response:

You can join the tables based on the user id and login time and then count the uploads:

SELECT u.user_id,
       u.login_datetime,
       u.logout_datetime,
       coalesce(count(f.id), 0) total_files_uploaded
  FROM user_metrics u
  LEFT JOIN files_uploaded f
    ON u.user_id = f.user_id
   AND f.file_uploaded_datetime BETWEEN u.login_datetime AND u.logout_datetime
 GROUP BY u.user_id, u.login_datetime, u.logout_datetime;

Output:

user_id login_datetime logout_datetime total_files_uploaded
1 2022-05-04 00:25:16 2022-05-04 00:30:16 1
3 2022-05-04 10:29:16 2022-05-04 10:40:16 1
1 2022-05-04 11:48:16 2022-05-04 11:49:56 1
3 2022-05-04 11:58:16 2022-05-04 12:20:16 3
1 2022-05-04 16:28:16 2022-05-04 17:29:56 4

If a user did not upload any files during their login, this will show 0. If you do not want to show entries for users who did not upload any files you can replace the left join with an inner join and remove the coalesce.

  • Related