Given a table Logs containing unique log IDs, write a SQL query to find the start and end number of continuous ranges in the log IDs.
A continuous range is a sequence of consecutive integers. For example, if we have log IDs 1, 2, 3, 7, 8, then we have two continuous ranges: [1, 3] and [7, 8].
Requirements:
- Return the result table with columns
start_idandend_id - Order the results by
start_id - Each row represents one continuous range
Table Schema
| Column Name | Type | Description |
|---|---|---|
log_id
PK
|
int | Unique log identifier |
Input & Output
| log_id |
|---|
| 1 |
| 2 |
| 3 |
| 7 |
| 8 |
| start_id | end_id |
|---|---|
| 1 | 3 |
| 7 | 8 |
The log IDs 1, 2, 3 form one continuous range from 1 to 3. The log IDs 7, 8 form another continuous range from 7 to 8. There's a gap between 3 and 7, so they form separate ranges.
| log_id |
|---|
| 1 |
| 3 |
| 6 |
| 7 |
| start_id | end_id |
|---|---|
| 1 | 1 |
| 3 | 3 |
| 6 | 7 |
Individual log IDs 1 and 3 each form their own range where start_id equals end_id. Log IDs 6, 7 are consecutive and form a range from 6 to 7.
| log_id |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| start_id | end_id |
|---|---|
| 1 | 5 |
All log IDs from 1 to 5 are consecutive, forming a single continuous range from 1 to 5.
Constraints
-
1 ≤ log_id ≤ 10^8 -
All
log_idvalues are unique - The table contains at least 1 row