In MySQL 8 You can use the window function LAG, to get the difference between two login Dates
CREATE TABLE User_roles (
`User_ID` INTEGER,
`Type` VARCHAR(10)
);
INSERT INTO User_roles
(`User_ID`, `Type`)
VALUES
('1', 'Admin'),
('2', 'EE'),
('3', 'Contractor'),
('5', 'Admin');
CREATE TABLE User_Sessions (
`User_ID` INTEGER,
`Session_ID` INTEGER,
`Session_created_at` VARCHAR(10)
);
INSERT INTO User_Sessions
(`User_ID`, `Session_ID`, `Session_created_at`)
VALUES
('1', '8', '2021-01-01'),
('1', '9', '2021-01-02')
,
('1', '18', '2021-01-20'),
('3', '10', '2021-01-03'),
('5', '11', '2021-01-04'),
('5', '12', '2021-01-05')
,
('5', '13', '2021-01-10');
SELECT
`Type` as 'User Type',
AVG(diff_days ) As 'AVG days between login'
FROM (SELECT ur.`User_ID`, ur.`Type`,
timestampdiff(DAY, lag(us.`Session_created_at`, 1) OVER (PARTITION BY us.`User_ID` ORDER BY us.`Session_ID` ASC), us.`Session_created_at`) as diff_days
FROM
User_Sessions us JOIN User_roles ur ON us.User_ID = ur.User_ID) t1
WHERE diff_days IS NOT NULL
GROUP BY `User_ID`,`Type`
ORDER BY `User_ID` ASC
User Type | AVG days between login
:-------- | ---------------------:
Admin | 9.5000
Admin | 3.0000