Presto SQL 时间日期函数
時(shí)間&日期函數(shù)
-- 2021-04-15 14:40:18.791 Asia/Shanghai select now()-- 2021-04-15 select current_date-- 14:10:41.586 Asia/Shanghai select current_time-- 2021-04-15 14:13:03.720 Asia/Shanghai select current_timestamp-- 昨天的年月日 select current_date - interval '1' day;-- Asia/Shanghai select current_timezone()-- 輸出long類型timestamp:1610085216 select to_unixtime(cast('2021-01-08 13:53:36' as timestamp)) -- 2021-01-08 select cast('2021-01-08' as date)-- 2021-01-08 00:00:00.000 select cast('2021-01-08' as timestamp)-- 2021-01-10 00:00:00.000 select date_parse('20210110','%Y%m%d')-- 輸出date類型日期:2021-01-08 select date(cast('2021-01-08 10:36:15' as timestamp)) select date(cast('2021-01-08' as timestamp)) -- 輸出varchar類型日期:2021-01-10 select format_datetime(date_parse('20210110','%Y%m%d'),'yyyy-MM-dd')-- 輸出varchar類型日期:2021-01-08 select format_datetime(from_unixtime(to_unixtime(cast('2021-01-08 13:53:36' as timestamp))),'yyyy-MM-dd') select format_datetime(from_unixtime(to_unixtime(cast('2021-01-08' as timestamp))),'yyyy-MM-dd')-- 輸出varchar類型日期:2021-01-08 13:53:36 select format_datetime(from_unixtime(1610085216),'yyyy-MM-dd HH:mm:ss') select format_datetime(from_unixtime(1610085216),'yyyy-MM-dd') -- 輸出date類型日期:2021-01-08 select date_add('day',-2,cast('2020-01-10' as date))-- 輸出date類型日期:2021-04-13 select date_add('day',-2,current_date) select date_add('week',-2,current_date) select date_add('month',-2,current_date) select date_add('quarter',-2,current_date) select date_add('year',-2,current_date)select date_add('millisecond',-2,date_parse('20210110','%Y%m%d')) select date_add('second',-2,date_parse('20210110','%Y%m%d')) select date_add('minute',-2,date_parse('20210110','%Y%m%d')) select date_add('hour',-2,date_parse('20210110','%Y%m%d')) -- date_diff(unit, timestamp1, timestamp2) → bigint select date_diff('day','2021-04-10','2021-04-15')select date_diff('day',date_parse('20210105','%Y%m%d'),date_parse('20210110','%Y%m%d'))select date_diff('day',cast('2020-01-05' as date),cast('2020-01-10' as date)) select date_diff('month',cast('2020-01-05' as date),cast('2020-01-10' as date))Presto SQL 特殊用法與進(jìn)階函數(shù)
1 try 函數(shù) 與 try_cast 函數(shù)
try(expression)try函數(shù)會(huì)其中表達(dá)式的異常情況,并將異常值返回為 NULL,如果不使用try函數(shù),則語句出現(xiàn)異常時(shí)將直接報(bào)錯(cuò)導(dǎo)致查詢失敗
您還可以搭配coalesce函數(shù)使用特定值替換 NULL 值,比如下列,將字段a轉(zhuǎn)換為整數(shù),轉(zhuǎn)換失敗則可轉(zhuǎn)換為 0
coalesce(try(cast('a' as integer)), 0)以上類型轉(zhuǎn)換可以使用 try_cast 函數(shù)來實(shí)現(xiàn),try_cast的作用與cast函數(shù)一致,都是對(duì)值進(jìn)行類型轉(zhuǎn)換,區(qū)別在于try_cast在類型轉(zhuǎn)換錯(cuò)誤時(shí)會(huì)返回 NULL,避免造成查詢失敗
coalesce(try_cast('a' as integer), 0)2 時(shí)間/日期函數(shù)
使用current_date、current_time、current_timestamp、localtime與localtimestamp時(shí)不用加圓括號(hào),Presto 也不支持加圓括號(hào)的寫法,使用時(shí)請(qǐng)注意
2.1 字符串與時(shí)間的轉(zhuǎn)換
可以直接在字符串格式的時(shí)間表達(dá)式前加關(guān)鍵字timestamp,比如timestamp '2020-01-01 00:00:00',即可直接獲得對(duì)應(yīng)的時(shí)間
date_parse與date_format分別是字符串轉(zhuǎn)時(shí)間以及時(shí)間轉(zhuǎn)字符串,用法都是傳入需要轉(zhuǎn)化的字段以及對(duì)應(yīng)的 format,如下分別是字符串$part_date轉(zhuǎn)時(shí)間以及時(shí)間#event_time轉(zhuǎn)字符串:
date_parse("$part_date", '%Y-%m-%d') date_format("#event_time", '%Y-%m-%d %T')以上函數(shù)的 format 格式使用的是 MySQL 的格式,如需使用 JAVA 格式,可使用函數(shù)format_datetime與parse_datetime
2.2 時(shí)間計(jì)算函數(shù)
函數(shù)date_add可對(duì)時(shí)間進(jìn)行偏移,unit為單位,value為偏移量,如果value為負(fù)數(shù),則為往前偏移
date_add(unit, value, timestamp)函數(shù)date_diff,用來計(jì)算兩個(gè)時(shí)間的差值,算法是timestamp2 - timestamp1,返回的是單位為unit的整數(shù)
date_diff(unit, timestamp1, timestamp2)兩個(gè)函數(shù)的unit的取值范圍,可以參考以下表格
| millisecond | 毫秒 |
| second | 秒 |
| minute | 分鐘 |
| hour | 小時(shí) |
| day | 日 |
| week | 周 |
| month | 月 |
| quarter | 季度 |
| year | 年 |
3 開窗函數(shù)
Presto 支持開窗函數(shù),窗口函數(shù)中有不少非常實(shí)用的函數(shù),如 first_value 與 last_value 很適合計(jì)算一段時(shí)間內(nèi)第一次或最后一次做某事的值。
比如計(jì)算每名用戶首次產(chǎn)生購買商品行為時(shí)購買的物品:
SELECT user_id,first_purchase_product FROM (SELECT user_id,first_value(product_name) over(partition by user_id order by time) AS first_purchase_product FROM log.purchase) GROUP BY user_id,first_purchase_productfirst_value與last_value需要搭配over子句使用,over子句中的partition by類似于group by,即按照所給字段分組,order by則會(huì)決定進(jìn)行排序的字段
4 JSON 解析
在一些特殊場(chǎng)景下,我們建議通過字符串的方式記錄復(fù)雜的數(shù)據(jù)結(jié)構(gòu),上傳到后臺(tái)。那么在之后的使用過程中,可以使用 JSON 解析函數(shù)轉(zhuǎn)化或提取能在 SQL 中使用的數(shù)據(jù)
4.1 字符串轉(zhuǎn) JSON
json_parse可以將符合 JSON 格式的字符串轉(zhuǎn)成 SQL 中的 JSON 類型數(shù)據(jù),兩函數(shù)等價(jià):
json_parse('{"abc":[1, 2, 3]}')4.2 JSON 轉(zhuǎn)其他類型
轉(zhuǎn)化為 JSON 的數(shù)據(jù),可以通過CAST轉(zhuǎn)化為其他 SQL 類型的數(shù)據(jù),比如將剛剛轉(zhuǎn)成 JSON 的字符串再次轉(zhuǎn)成 MAP:
CAST(json_parse('{"abc":[1, 2, 3]}') AS MAP(varchar,array(integer)))如果希望將字符串重新轉(zhuǎn)成 JSON,可以使用json_format:
json_format(json_parse('{"abc":[1, 2, 3]}'))4.3 直接提取 JSON 數(shù)據(jù)
在很多情況下,只需要提取 JSON 中的部分?jǐn)?shù)據(jù)即可,此時(shí)可以使用json_extract_scalar進(jìn)行提取,其通過 JSONPath 表達(dá)式進(jìn)行提取,返回字符串:
json_extract_scalar(json, json_path)并且json_extract_scalar可以直接對(duì) JSON 的字符串進(jìn)行提取,無需手動(dòng)轉(zhuǎn)化為 JSON 類型,比如以下,提取abc的第一個(gè)元素:
json_extract_scalar('{"abc":[1, 2, 3]}','$.abc[0]')總結(jié)
以上是生活随笔為你收集整理的Presto SQL 时间日期函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MSP430F5529 DriverLi
- 下一篇: SQL常用日期函数