Shilpa
1 Mar 2022
PHP & Mysql
When any user logged in to my web application, I want to count the streak for how many days continuously that user is logged in.
Here is my data: Live sample link
Table schema:
CREATE TABLE mytable
(`Id` int, `UserId` int, `Timestamp` date, `IsLoggedIn` tinyint)
;
Table data:
INSERT INTO mytable
(`Id`, `UserId`, `Timestamp`, `IsLoggedIn`)
VALUES
(1, 1, '2022-02-28',1),
(2, 1, '2022-02-27',1),
(3, 2, '2022-02-28',1),
(4, 1, '2022-03-01',1),
(5, 1, '2022-02-26',1),
(6, 2, '2022-02-26',1),
(7, 3, '2022-02-25',1),
(8, 2, '2022-03-01',1),
(9, 3, '2022-03-01',1)
;
and select query,
select * from mytable;
I want to count the streak of logged-in user.
For example UserId = 1, total day streak should be 4, as that user logged in on Feb 26 to Mar 01 (02/26, 02/27, 02/28, 03/01).
How do I calculate among the entire table?
Eslam Zedan
2 Mar 2022
One way to do this is to use MySQL user variables. This isn't necessarily the most efficient approach for large sets, since it materializes two inline views.
SELECT s.UserId
, MAX(s.streak) AS longest_streak
FROM ( SELECT IF(@prev_user = o.user AND o.Timestamp = @prev_date + INTERVAL 1 DAY
, @streak := @streak + 1
, @streak := 1
) AS streak
, @prev_user := o.user AS user
, @prev_date := o.Timestamp AS `date`
FROM ( SELECT t.user
, DATE(t.Timestamp) AS `date`
FROM mytable t
CROSS
JOIN (SELECT @prev_user := NULL, @prev_date := NULL, @streak := 1) i
GROUP BY t.UserId, DATE(t.Timestamp)
ORDER BY t.UserId, DATE(t.Timestamp)
) o
) s
GROUP BY s.UserId
The inline view aliased as i just initializes some user variables; we don't really care what it returns, except that we need it to return exactly 1 row because of the JOIN operation; we just really care about the side effect of initializing user variables early in the statement execution.
The inline view aliased as o gets a list of users and dates; the specification was for an entry "on each date", so we can truncate off the time portion, and get just the DATE, and make that into a distinct set, using the GROUP BY clause.
The inline view aliased as s processes each row, and saves the values of the current row into the @prev_
user variables. Before it overwrites the values, it compares the values on the current row to the values (saved) from the previous row. If the user matches, and the date on the current row is exactly 1 day later than the previous date, we are continuing a "streak", so we increment the current value of the @streak
variable by 1. Otherwise, the previous streak was broken, and we start a new "streak", resetting @streak
to 1.
Finally, we process the rows from s to extract the maximum streak for each user.
(This statement is desk checked only, there could be a typo or two.)
as I found here
The query you provided is not working.
I updated the same query as below, but still it returns error.
SELECT s.UserId
, MAX(s.streak) AS longest_streak
FROM ( SELECT IF(@prev_user = o.UserId AND o.Timestamp = @prev_date + INTERVAL 1 DAY
, @streak := @streak + 1
, @streak := 1
) AS streak
, @prev_user := o.UserId AS user
, @prev_date := o.Timestamp AS `date`
FROM ( SELECT t.UserId
, DATE(t.Timestamp) AS `date`
FROM mytable t
CROSS
JOIN (SELECT @prev_user := NULL, @prev_date := NULL, @streak := 1) i
GROUP BY t.UserId, DATE(t.Timestamp)
ORDER BY t.UserId, DATE(t.Timestamp)
) o
) s
GROUP BY s.UserId
Thank you for responding, but can you please check?
Rakshit
2 Mar 2022
Best Answer
Here I created a MySQL query for your question. Using CROSS JOIN to same table you can achieve your goals. I am calculating time from today's date to a continuous streak.
-- Query to get a streak of days for userId = 1; (visit: //sqlfiddle.com/#!9/9e8371/1/0)
SELECT MAX(streak) AS streak
FROM (
SELECT Id, UserId, `Timestamp`,
DATEDIFF(NOW(), `Timestamp`),
@streak := IF( DATEDIFF(NOW(), `Timestamp`) - @days_diff > 1, @streak,
IF(@days_diff := DATEDIFF(NOW(), `Timestamp`), @streak+1, @streak+1)) AS streak
FROM mytable
CROSS JOIN (SELECT @streak := 0, @days_diff := -1) AS vars
WHERE `Timestamp` <= NOW() AND UserId = 1
ORDER BY `Timestamp` DESC) AS t;
Reference: SQLFiddle
© 2023 Copyrights reserved for web-brackets.com