【DataPM】Impala里的日期函數(shù)

最近Impala里查數(shù)據(jù)的時(shí)候弓摘,發(fā)現(xiàn)有很多時(shí)間函數(shù)不支持焚鹊,因此,搜集整理了一批常用時(shí)間函數(shù)韧献,以備查詢末患。

讓日期自己與自己比較大小

datediff(first_value(create_time) over(partition by create_user order by create_time),create_time)

這是利用orcale里的窗口函數(shù)研叫,first_value () ?over(partition by ? ? order by ? )意思是先將creat_time按照create_user分組,再按create_time進(jìn)行排序璧针,然后first_value()取出第一個(gè)時(shí)間嚷炉,也就是最小時(shí)間,然后與create_time進(jìn)行比較探橱,通過(guò)得到的數(shù)值比較大小申屹。

有什么應(yīng)用場(chǎng)景呢?

若datediff()=0隧膏,那就表示只在當(dāng)天哗讥,可以用來(lái)判斷是否是只在當(dāng)天有數(shù)據(jù),也就是新增數(shù)據(jù)胞枕。

增加月份

add_months(timestamp date, int months)

add_months(timestamp date, bigint months)

Return type: timestamp

usage:add_months(now(),1)

增加日期

adddate(timestamp startdate, int days),

adddate(timestamp startdate, bigint days)

Return type: timestamp

usage:adddate(now(),1)

當(dāng)前時(shí)間戳

current_timestamp()和now()等價(jià)

日期相減

datediff(string enddate, string startdate)

Return type: int

usage:datediff("2018-08-05", "2018-08-03")

得到天杆煞,得到月份

day(string date)

Return type: int

usage: day("2018-08-05")

得到星期英文

dayname(string date)

Return type: string

usage:dayname("2018-08-05") Sunday

得到這一天是這周的第幾天

dayofweek(string date)? 1 (Sunday) to 7 (Saturday).

Return type: int

usage:dayofweek("2018-08-06")

加天數(shù)

days_add(timestamp startdate, int days)

Return type: timestamp

usage:days_add(now(),2)

減天數(shù)

days_sub(timestamp startdate, int days)

Return type: timestamp

usage:days_sub(now(), 2)

格式化日期

from_unixtime(bigint unixtime[, string format])

Return type: string

注意參數(shù)

usage:from_unixtime(1392394861,"yyyy-MM-dd");

得到小時(shí)

hour(string date)

Return type: int

usage:hour("2018-08-06 12:32:54")

增加小時(shí)

hours_add(timestamp date, int hours)

Return type: timestamp

usage:hours_add(now(),2)

減少

hours_sub(timestamp date, int hours)

Return type: timestamp

usage:hours_sub(now(),2)

得到分鐘

minute(string date)

Return type: int

usage:minute(now())

增加分鐘

minutes_add(timestamp date, int minutes)

Return type: timestamp

usage:minutes_add(now(),2)

減少分鐘

minutes_sub(timestamp date, int minutes)

Return type: timestamp

usage:minutes_sub(now(),2)

得到月份

month(string date)

Return type: int

usage:month("2018-08-06 12:32:54")

月份相加

months_add(timestamp date, int months)

Return type: timestamp

usage:months_add(now(),3)

減月份

months_sub(timestamp date, int months)

Return type: timestamp

months_sub(now(),3)

得到秒

second(string date)

Return type: int

秒加

seconds_add(timestamp date, int seconds)

Return type: timestamp

秒減

seconds_sub(timestamp date, int seconds)

Return type: timestamp

得到日期

to_date(now())

得到1970到今秒

unix_timestamp(),

unix_timestamp(string datetime),

unix_timestamp(string datetime, string format),

unix_timestamp(timestamp datetime)

Return type: bigint

得到這周是這年的多少周

weekofyear(string date)

Return type: int

usage:weekofyear("2018-08-06 12:32:54")

周加

weeks_add(timestamp date, int weeks)

Return type: timestamp

usage:weeks_add("2018-08-06 12:32:54", 1)

周減

weeks_sub(timestamp date, int weeks)

Return type: timestamp

usage:weeks_sub("2018-08-06 12:32:54", 1)

得到年

year(string date)

Return type: int

年加

years_add(timestamp date, int years)

Return type: timestamp

年減

years_sub(timestamp date, int years)

Return type: timestamp

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市腐泻,隨后出現(xiàn)的幾起案子索绪,更是在濱河造成了極大的恐慌,老刑警劉巖贫悄,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異娘摔,居然都是意外死亡窄坦,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門凳寺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)鸭津,“玉大人,你說(shuō)我怎么就攤上這事肠缨∧媲鳎” “怎么了?”我有些...
    開封第一講書人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵晒奕,是天一觀的道長(zhǎng)闻书。 經(jīng)常有香客問(wèn)我,道長(zhǎng)脑慧,這世上最難降的妖魔是什么魄眉? 我笑而不...
    開封第一講書人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮闷袒,結(jié)果婚禮上坑律,老公的妹妹穿的比我還像新娘。我一直安慰自己囊骤,他們只是感情好晃择,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開白布冀值。 她就那樣靜靜地躺著,像睡著了一般宫屠。 火紅的嫁衣襯著肌膚如雪列疗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評(píng)論 1 312
  • 那天激况,我揣著相機(jī)與錄音作彤,去河邊找鬼。 笑死乌逐,一個(gè)胖子當(dāng)著我的面吹牛竭讳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浙踢,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼绢慢,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了洛波?” 一聲冷哼從身側(cè)響起胰舆,我...
    開封第一講書人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蹬挤,沒(méi)想到半個(gè)月后缚窿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡焰扳,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年倦零,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吨悍。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡扫茅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出育瓜,到底是詐尸還是另有隱情葫隙,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布躏仇,位于F島的核電站恋脚,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏钙态。R本人自食惡果不足惜慧起,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望册倒。 院中可真熱鬧蚓挤,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至缤剧,卻和暖如春馅袁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荒辕。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工汗销, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人抵窒。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓弛针,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親李皇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子削茁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容