Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 직장인자기계발
- group by
- meidum
- 패캠챌린지
- LeetCode
- 직장인인강
- SQL
- 시계열데이터분석
- 해커랭크
- RANK
- 프로그래머스
- 파이썬을활용한시계열데이터분석
- solvesql
- Hard
- 파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
- HACKER_RANK
- 프리미엄
- easy
- 다시풀어보기
- lv.4
- 패스트캠퍼스후기
- MySQL
- recursive
- 어려웠음
- medium
- row_number
- 패스트캠퍼스
- Hackerrank
- SELF-JOIN
- join
Archives
- Today
- Total
~고군분투 인생살이~
[180_MEDIUM] Consecutive Numbers 다시풀기 본문
Table: Logs
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| num | varchar |
+-------------+---------+
id is the primary key for this table.
id is an autoincrement column.
Write an SQL query to find all numbers that appear at least three times consecutively.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
Output:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
Explanation: 1 is the only number that appears consecutively for at least three times.
문제 조건
연속적으로 세번 이상 나타나는 숫자를 찾으시오
쿼리
# windowfunction
select distinct l.num as ConsecutiveNums
from (select num,
lead(num,1) over(order by id asc) as next,
lead(num,2) over(order by id asc) as after_next
from logs) l
where l.num = l.next
and l.num = l.after_next
# self-join
select distinct l1.num as ConsecutiveNums
from logs l1
join logs l2 on l1.id+1 = l2.id
join logs l3 on l2.id+1 = l3.id
where l1.num = l2.num
and l1.num = l3.num
'SQL > LEETCODE' 카테고리의 다른 글
[608_MEDIUM] Tree Node 다시풀기 (0) | 2022.09.26 |
---|---|
[184_MEDIUM] Department Highest Salary 다시풀기 (0) | 2022.09.26 |
[197_EASY] Rising Temperature (0) | 2022.09.25 |
[183_EASY] Customers Who Never Order (0) | 2022.09.25 |
[178_MEDIUM] Rank Scores (0) | 2022.09.23 |
Comments