달나라 노트

Presto : date_diff() (두 시점 간의 차이 구하기, 시간 차이) 본문

SQL/Presto

Presto : date_diff() (두 시점 간의 차이 구하기, 시간 차이)

CosmosProject 2023. 10. 10. 21:55
728x90
반응형

 

 

 

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) 의 값을 계산하기 때문에 음수 값이 나옵니다.

 

 

 

 

 

 

728x90
반응형
Comments