For each user with at least two logins, compute the average interval in days between consecutive logins, rounded to 2 decimal places. Return user_id and avg_interval_days, ordered by user_id ascending.
Input Table(s)
user_login
| Column | Type | Meaning |
|---|
| id | INT | Login record identifier (primary key, auto-increment) |
| user_id | INT | User who logged in, NOT NULL |
| login_time | TIMESTAMP | Date and time of the login, NOT NULL |
Keys: id is the primary key. A user has one row per login.
Output Contract
| Column | Type | Meaning |
|---|
| user_id | INT | User with at least two logins |
| avg_interval_days | NUMERIC | Mean gap between consecutive logins in fractional days, rounded to 2 decimals |
One row per qualifying user, ordered by user_id ascending.
Example
user_login
| id | user_id | login_time |
|---|
| 1 | 1 | 2024-01-01 08:00:00 |
| 2 | 1 | 2024-01-03 10:00:00 |
| 3 | 1 | 2024-01-06 10:00:00 |
| 4 | 2 | 2024-01-01 09:00:00 |
| 5 | 2 | 2024-01-04 09:00:00 |
| 6 | 3 | 2024-01-01 12:00:00 |
Expected result:
| user_id | avg_interval_days |
|---|
| 1 | 2.54 |
| 2 | 3.00 |
User 1 has intervals of 2.083 days (Jan 1 08:00 to Jan 3 10:00) and 3.000 days (Jan 3 to Jan 6), which average to 2.54 days. User 2 has one interval of 3 days. User 3 has only one login, so it is excluded.
Constraints and Clarifications
-
user_login
has 1 to 10,000 rows, and
user_id
is a positive integer.
-
Consecutive means adjacent in
login_time
order for the same user. The order of
id
values is not meaningful.
-
Intervals are measured in fractional days (elapsed seconds divided by 86,400), not as differences between calendar dates.
-
login_time
is not necessarily unique per user. Two logins at the same time are still two logins, and the interval between them is 0.
-
Round only the final average, not each interval.