set @a = ''; # Previous username variable
set @c = ''; # Previous timestamp variable
set @d = ''; # Grouping timestamp variable
set @e = 0; # Timestamp counter variable
SELECT
IF(@a != username, @e := 0, @e := @e + IFNULL((ABS(TIMESTAMPDIFF(SECOND, timestamp, @c))), 0)) AS time_counter,
IF((ABS(TIMESTAMPDIFF(SECOND, timestamp, @c)) > 30 OR @c = '') OR
(@a != username) OR
(@e > 30), @d := timestamp, @d := @d) as timestamp_groups,
IF(@a != username OR @e > 30, @e := 0, @e := @e) AS counter_reset,
@c as timestamp_previous,
username, timestamp, points,
@a := username username_calc, @c := timestamp timestamp_calc,
CONCAT(@a, CAST(@d AS CHAR(20))) AS user_grouping
FROM points_table
ORDER BY username, timestamp