33. Find Continuous Ranges in Logs
We have a table named Logs that contains log IDs.
Table: Logs
โโโโโโโโโโโโโโโโโฆโโโโโโโโโโ โ Column Name โ Type โ โ โโโโโโโโโโโโโโโโฌโโโโโโโโโโฃ โ log_id โ int โ โโโโโโโโโโโโโโโโโฉโโโโโโโโโโ
log_id is the primary key and contains unique values for this table.
Each row in this table represents a log with a unique log_id.
Task:
Write a query to find the start and end number of continuous ranges in the Logs table.
Return the result table ordered by start_id.
Example 1:
Example
Input:
Logs table:
โโโโโโโโโโโโโโ โ log_id โ โ โโโโโโโโโโโโโฃ โ 1 โ โโโโโโโโโโโโโโ โ 2 โ โโโโโโโโโโโโโโ โ 3 โ โโโโโโโโโโโโโโ โ 7 โ โโโโโโโโโโโโโโ โ 8 โ โโโโโโโโโโโโโโ โ 10 โ โโโโโโโโโโโโโโ
Output:
โโโโโโโโโโโโโโฆโโโโโโโโโโโโโโโ โ start_id โ end_id โ โ โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฃ โ 1 โ 3 โ โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ โ 7 โ 8 โ โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ โ 10 โ 10 โ โโโโโโโโโโโโโโฉโโโโโโโโโโโโโโโ
Explanation:
The range from 1 to 3 is continuous in the table.
The range from 7 to 8 is continuous in the table.
The number 10 is a single value and thus forms its own range.
Numbers from 4 to 6 and 9 are missing in the table, so they are not included.
Still unsure what the problem is asking ?
Letโs go through a few more examples, step by step, to make it clearer.