일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- google apps script
- dataframe
- Apache
- hive
- SQL
- PANDAS
- Github
- gas
- c#
- 파이썬
- Java
- PostgreSQL
- Mac
- Google Spreadsheet
- Excel
- string
- django
- array
- Redshift
- matplotlib
- Kotlin
- math
- numpy
- Google Excel
- GIT
- Python
- PySpark
- Tkinter
- list
- Today
- Total
달나라 노트
Hive : unix_timestamp를 이용해서 두 시점간의 차이 구하기 본문
두 시점간의 차이가 어느 정도인지를 구하는 방법은 여러 가지가 있겠지만 오늘은 unix_timestamp를 이용해 보겠습니다.
unix_timestamp 함수는 어떤 timestamp 또는 timestamp_string을 unix_timestamp로 변환해주는 역할을 합니다.
https://cosmosproject.tistory.com/50
Hive : unix_timestamp & from_unixtime (yyyymmdd 형태의 텍스트를 date type으로 바꾸기. 날짜와 unix_timestamp간의
unix_timestamp()의 사용법은 아래와 같습니다. unix_timestamp(날짜를 나타내는 텍스트, 날짜의 형식) 예시를 한번 봐봅시다. select unix_timestamp('2020-11-10', 'yyyy-MM-dd'); 결과 : 1604934000 위 식을 보면 날짜를
cosmosproject.tistory.com
Unix timestamp는 1970년 1월 1일(UTC)부터 몇 초가 흘렀는지를 나타내는 수치입니다.
원리는 2개의 timestamp를 각각 초 단위인 unix timestamp로 바꾸고 그것을 빼서 두 시점간의 차이를 구하는 것입니다.
select unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss') as start_unix_timestamp
, unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') as end_unix_timestamp
, unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss') as second_diff
;
-- Result
start_unix_timestamp = 1702189432
end_unix_timestamp = 1702381512
second_diff = 192080
위 예시는 두 시점간의 unix
- unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss') as start_unix_timestamp
2023-12-10 15:23:52 를 unix timestamp로 바꾸면 1702189432 입니다.
- unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') as end_unix_timestamp
2023-12-12 20:45:12 를 unix timestamp로 바꾸면 1702381512 입니다.
- unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss') as second_diff
위 두 시점간의 차이를 구하면 192080초 입니다.
select unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss') as start_unix_timestamp
, unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') as end_unix_timestamp
, unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss') as second_diff
, (unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss')) / 60 as minute_diff
, (unix_timestamp('2023-12-12 20:45:12', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2023-12-10 15:23:52', 'yyyy-MM-dd HH:mm:ss')) / 60 / 60 as hour_diff
;
-- Result
start_unix_timestamp = 1702189432
end_unix_timestamp = 1702381512
second_diff = 192080
minute_diff = 3201.33333333
hour_diff = 53.35555556
unix_timestamp는 초단위로 나타내어지기 때문에 unix_timestamp의 차이도 초단위입니다.
이렇게 구해진 두 시점간의 초단위 차이값을 60으로 나누면 분으로 바꿀 수 있고 (= minute_diff)
그걸 또 60으로 나누면 시간으로 바꿀 수 있습니다. (= hour_diff)
이런식으로 두 시점간의 차이를 구할 수 있습니다.
'SQL > Apache Hive' 카테고리의 다른 글
Hive : split (특정 구분자로 문자열 나누기, 구분자 문자열 나누기) (0) | 2023.11.28 |
---|---|
Hive : concat() (문자열 연결하기) (0) | 2023.10.13 |
Hive : datediff (두 날짜 간의 차이 구하기, 두 시점 차이를 일단위로 구하기) (0) | 2023.10.10 |
Hive : extract (날짜에서 특정 요소 추출하기, 시간 추출, 년도 추출, 주차 추출, 요일 추출) (0) | 2023.10.10 |
Hive : dayofmonth, dayofweek, weekofyear (일 추출, 요일 추출, 주차 번호 추출, weeknumber, day, weekday) (0) | 2023.10.10 |