코딩테스트 SQL 코드카나 (Rising Temperature)

2024. 11. 18. 09:23·Daily 코드카타/SQL

문제

Rising Temperature - LeetCode

 

 

Write a solution to find all dates' id with higher temperatures compared 
to its previous dates (yesterday).

Return the result table in any order.

The 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).

 

 


풀이

  1. recordDate에서 전날짜와 비교하여 temperature이 높은경우 id를 프린트

 


코드

우선 자가 조인을 한다. w2.recordDate에 1을 더한날이 w1.recordDate로 설정하여서

w1을 오늘, w2를 어제로 한다.

 

DATE_ADD를 사용하여 interval로 하루씩 올린다.

 

 

즉 오늘의 온도가 어제보다 높으면 표시

 

select w1.id
from Weather w1
join Weather w2
on w1.recordDate = DATE_ADD(w2.recordDate, interval 1 day)
where w1.temperature > w2.temperature

 

 


링크

LeetCode/0197-rising-temperature at main · K-MarkLee/LeetCode

 

LeetCode/0197-rising-temperature at main · K-MarkLee/LeetCode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub) - K-MarkLee/LeetCode

github.com

 

저작자표시 비영리 변경금지 (새창열림)

'Daily 코드카타 > SQL' 카테고리의 다른 글

코딩테스트 SQL 코드카타 (Employee Bonus)  (1) 2024.11.20
코딩테스트 SQL 코드카타 (Average Time of Process per Machine)  (0) 2024.11.19
코딩테스트 SQL 코드카타 (Customer Who Visited without Any Transactions)  (0) 2024.11.17
코딩테스트 SQL 코드카타 (Product Sales Analysis)  (0) 2024.11.17
코딩테스트 SQL 코드카타 (Replace Employee ID)  (1) 2024.11.15
'Daily 코드카타/SQL' 카테고리의 다른 글
  • 코딩테스트 SQL 코드카타 (Employee Bonus)
  • 코딩테스트 SQL 코드카타 (Average Time of Process per Machine)
  • 코딩테스트 SQL 코드카타 (Customer Who Visited without Any Transactions)
  • 코딩테스트 SQL 코드카타 (Product Sales Analysis)
코드 유랑자 승열
코드 유랑자 승열
코드 유랑자 승열의 프로그래밍 일지를 남기는 공간입니다.
  • 코드 유랑자 승열
    승열의 프로그래밍 시네마
    코드 유랑자 승열
  • 전체
    오늘
    어제
  • 링크

    • 깃허브 보러가기
    • 링크드인 보러가기
    • 인스타그램 보러가기
    • 카테고리
      • 코딩테스트
        • BaekJoon
      • TIL and WIL
        • TIL
        • WIL
      • 주말스터디
      • 내일배움캠프
        • 사전캠프 강의 (SQL)
      • 용어정리
        • Python
        • Python-Library
        • Machine-Learning
        • Deep-Learning
        • AI 활용
        • LLM & RAG
        • Docker
        • Django
        • SQL
        • Java Script
        • etc
      • Daily 코드카타
        • SQL
        • Python 알고리즘
      • 임시저장
      • 보류
  • 태그

    오블완
    word2vec
    RAG
    template
    llm
    vector db
    티스토리챌린지
    View
    langchain
    django
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코드 유랑자 승열
코딩테스트 SQL 코드카나 (Rising Temperature)
상단으로

티스토리툴바