-
leetcode - 1285. Find the Start and End Number of Continuous RangesSQL 문제풀이 2022. 8. 3. 16:59
난이도 : Medium
Table: Logs
+---------------+---------+ | Column Name | Type | +---------------+---------+ | log_id | int | +---------------+---------+ log_id is the primary key for this table. Each row of this table contains the ID in a log Table.
Write an SQL query to find the start and end number of continuous ranges in the table Logs.
Return the result table ordered by start_id.
The query result format is in the following example.
Example 1:
Input: Logs table: +------------+ | log_id | +------------+ | 1 | | 2 | | 3 | | 7 | | 8 | | 10 | +------------+ Output: +------------+--------------+ | start_id | end_id | +------------+--------------+ | 1 | 3 | | 7 | 8 | | 10 | 10 | +------------+--------------+ Explanation: The result table should contain all ranges in table Logs. From 1 to 3 is contained in the table. From 4 to 6 is missing in the table From 7 to 8 is contained in the table. Number 9 is missing from the table. Number 10 is contained in the table.
SELECT MIN(log_id) start_id , MAX(log_id) end_id FROM ( SELECT log_id , DENSE_RANK() OVER (ORDER BY log_id) rnk FROM Logs ) sub GROUP BY log_id - rnk
Accepted (8.47%)
'SQL 문제풀이' 카테고리의 다른 글
leetcode - 1853. Convert Date Format (0) 2022.08.04 leetcode - 1308. Running Total for Different Genders (0) 2022.08.03 leetcode - 1821. Find Customers With Positive Revenue this Year (0) 2022.08.02 leetcode - 1809. Ad-Free Sessions (0) 2022.08.02 leetcode - 1789. Primary Department for Each Employee (0) 2022.08.02