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
- Hard
- medium
- 패캠챌린지
- HACKER_RANK
- 패스트캠퍼스후기
- 어려웠음
- RANK
- meidum
- LeetCode
- 시계열데이터분석
- 파이썬을활용한시계열데이터분석
- lv.4
- recursive
- 다시풀어보기
- easy
- 프리미엄
- row_number
- 직장인인강
- solvesql
- MySQL
- Hackerrank
- join
- group by
- SELF-JOIN
- 파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
- 프로그래머스
- 직장인자기계발
- SQL
- 해커랭크
- 패스트캠퍼스
Archives
- Today
- Total
~고군분투 인생살이~
[197_EASY] Rising Temperature 본문
Rising Temperature - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Table: Weather
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id is the primary key for this table.
This table contains information about the temperature on a certain day.
Write an SQL query to find all dates' Id with higher temperatures compared to its previous dates (yesterday).
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Weather table:
+----+------------+-------------+
| id | recordDate | temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+----+------------+-------------+
Output:
+----+
| id |
+----+
| 2 |
| 4 |
+----+
Explanation:
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).
문제 조건
이전 날짜와 비교해서 온도가 더 높은 날짜의 ID를 출력하시오
문제 풀이
to_days() : 주어진 날짜를 0000년부터의 일수로 바꾼다.
select to_days(950501) -> 728779
-- self join
select today.id as id
from weather yesterday
left join weather today on yesterday.recordDate = date_sub(today.recordDate,interval 1 day)
where yesterday.temperature < today.temperature
-- cross join (runtime이 오래걸림... cross join이라?)
select today.id as id
from weather as today
cross join weather as yesterday
where today.temperature > yesterday.temperature
and to_days(today.recordDate) - to_days(yesterday.recordDate) = 1
'SQL > LEETCODE' 카테고리의 다른 글
[184_MEDIUM] Department Highest Salary 다시풀기 (0) | 2022.09.26 |
---|---|
[180_MEDIUM] Consecutive Numbers 다시풀기 (0) | 2022.09.26 |
[183_EASY] Customers Who Never Order (0) | 2022.09.25 |
[178_MEDIUM] Rank Scores (0) | 2022.09.23 |
[177_MEDIUM] Nth Highest Salary (0) | 2022.09.23 |
Comments