日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Hive獲取當天0點時間,條件查詢某一天數(shù)據(jù)

作者:DangerShi 更新時間: 2022-07-12 編程語言

獲取當天0點的時間:

select from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00');

執(zhí)行結(jié)果為:

2022-07-08 00:00:00

獲取當天0點的秒級時間戳:

select unix_timestamp(from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00'));

執(zhí)行結(jié)果:

1657209600

將時間作為條件,條件查詢

查詢創(chuàng)建時間(格式為yyyy-MM-dd HH:mm:ss)在前一天0點到今天0點之間的數(shù)據(jù)

select *?from table_t where created_time >?from_unixtime(unix_timestamp()-86400,'yyyy-MM-dd 00:00:00') and created_time <=?from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00')

若條件字段為時間戳使用時間戳,則同理使用unix_timestamp(from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00'))。

以上。

PS:

補充hive時間日期函數(shù),備忘

以下轉(zhuǎn)自hive時間日期函數(shù)總結(jié) - 簡書

-- 1、hive取得當前日期時間:

-- 1.1) 取得當前日期:
select current_date();

-- 1.2) 取得當前日期時間:
select current_timestamp();

-- 1.3) hive取得當前時間戳:
select unix_timestamp();

-- 1.4) 時間戳轉(zhuǎn)日期:
select from_unixtime(1517725479,'yyyy-MM-dd HH:dd:ss');

-- 1.5) 日期轉(zhuǎn)unix時間戳:
select to_nuix_timestamp('2017-01-01 12:12:12','yyyy-MM-dd HH:dd:ss');

-- 1.6) hive取得當前時間:
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');

-- 2、hive自動計算其他日期(昨天,今天):
-- hive中日期加減函數(shù):date_add(start_date,num_days)

-- 2.1) 取得昨天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_format(date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);


-- 2.2) 取得明天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

-- 2.3)hive取得兩個日期之間差值(差值為天數(shù)):
-- datediff(date1,date2):date1大于date2,返回值為正,否則,返回值為負。

select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10));
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));

-- 2.4) 字符串轉(zhuǎn)時間(字符串必須為:yyyy-MM-dd格式)

select to_date('2017-01-01 12:12:12');

-- 2.5) 日期、時間戳、字符串類型格式化輸出標準時間格式:

select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
select date_format(current_date(),'yyyyMMdd');
select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss');       --字符串必須滿足yyyy-MM-dd格式

-- 2.6) utc時間轉(zhuǎn)換:

select from_utc_timestamp(current_timestamp(),8);
select to_utc_timestamp(current_timestamp(),8);

原文鏈接:https://blog.csdn.net/jian876601394/article/details/125681613

欄目分類
最近更新