일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Redshift
- SQL
- Tkinter
- Mac
- numpy
- list
- Github
- matplotlib
- Kotlin
- Google Spreadsheet
- PANDAS
- Apache
- django
- 파이썬
- array
- GIT
- c#
- math
- Java
- Excel
- gas
- dataframe
- hive
- Python
- PostgreSQL
- Google Excel
- string
- google apps script
- PySpark
- Today
- Total
달나라 노트
Presto : date_diff() (두 시점 간의 차이 구하기, 시간 차이) 본문
Presto의 date_diff() 함수는 두 시점간의 차이를 원하는 단위(e.g. 시간, 분 등)로 return합니다.
Syntax
date_diff(unit, start_datetime, end_datetime)
- unit
두 시점간의 차이를 어떤 단위로 return할지를 결정합니다.
year = 두 시점간의 차이를 년 단위로 계산합니다.
month = 두 시점간의 차이를 월 단위로 계산합니다.
day = 두 시점간의 차이를 일 단위로 계산합니다.
hour = 두 시점간의 차이를 시간 단위로 계산합니다.
minute = 두 시점간의 차이를 분 단위로 계산합니다.
second = 두 시점간의 차이를 초 단위로 계산합니다.
- start_datetime
시작 시점의 datetime입니다.
- end_datetime
종료 시점의 datetime입니다.
그리고 date_diff() 함수는 end_datetime - start_datetime 의 결과를 unit에 명시된 단위로 환산해서 return합니다.
select date_diff('year', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as year_diff
, date_diff('month', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as month_diff
, date_diff('day', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as day_diff
, date_diff('hour', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as hour_diff
, date_diff('minute', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as minute_diff
, date_diff('second', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as second_diff
-- Result
year_diff = 1
month_diff = 23
day_diff = 700
hour_diff = 16816
minute_diff = 1009007
second_diff = 60540420
timestamp는 timestamp string을 timestamp로 바꿔줍니다. (참고 = https://cosmosproject.tistory.com/423)
- date_diff('year', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as year_diff
2022-01-20 20:35:31
2023-12-22 13:22:31
위 두 시점간의 차이를 년도 단위로 return합니다.
위 두 날짜의 차이는 1년하고도 11~12개월정도 더 있지만 년 단위로 계산하므로 1년을 채우지 못하는 시간은 다 버립니다.
따라서 1(year)을 return합니다.
- date_diff('month', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as month_diff
위 두 날짜의 차이를 month 단위로 return합니다.
따라서 23(month)이 return됩니다.
- date_diff('day', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as day_diff
위 두 날짜의 차이를 day 단위로 return합니다.
따라서 700(day)이 return됩니다.
- date_diff('hour', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as hour_diff
위 두 날짜의 차이를 hour 단위로 return합니다.
따라서 16816(hour)이 return됩니다.
- date_diff('minute', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as minute_diff
위 두 날짜의 차이를 minute 단위로 return합니다.
따라서 1009007(minute)이 return됩니다.
- date_diff('second', timestamp '2022-01-20 20:35:31', timestamp '2023-12-22 13:22:31') as second_diff
위 두 날짜의 차이를 second 단위로 return합니다.
따라서 60540420(second)이 return됩니다.
select date_diff('year', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as year_diff
, date_diff('month', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as month_diff
, date_diff('day', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as day_diff
, date_diff('hour', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as hour_diff
, date_diff('minute', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as minute_diff
, date_diff('second', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as second_diff
-- Result
year_diff = -1
month_diff = -23
day_diff = -700
hour_diff = -16816
minute_diff = -1009007
second_diff = -60540420
이전에 본 예시와 동일하지만 start_timestamp와 end_timestamp의 위치를 서로 바꿨습니다.
- date_diff('year', timestamp '2023-12-22 13:22:31', timestamp '2022-01-20 20:35:31') as year_diff
2023-12-22 13:22:31
2022-01-20 20:35:31
위 두 시점의 차이를 year단위로 return합니다.
다만, (2023-12-22 13:22:31 - 2022-01-20 20:35:31) 의 값을 계산하기 때문에 음수 값이 나옵니다.
'SQL > Presto' 카테고리의 다른 글
Presto : date_add() (시간 더하기, 날짜 더하기, 특정 기간 더하기) (0) | 2023.10.11 |
---|---|
Presto : interval (날짜 시간 더하기. 날짜 시간 빼기) (0) | 2022.01.14 |
Presto : date, timestamp (날짜 형식의 문자를 date 형식으로 변경. 날짜 형식의 문자를 timestamp 형식으로 변경.) (0) | 2022.01.14 |
Presto : current_date (현재 날짜. 현재 날짜 return. Presto 현재 날짜. 오늘 날짜) (0) | 2022.01.14 |