Hive上計算兩時點相差分鐘數的方法
阿新 • • 發佈:2019-01-22
Hive比較特殊,計算兩時間點相差分鐘資料的方法是,先將兩時間字串轉化成時間格式,然後兩者相減,再分別將小時、分鐘、秒換算成分鐘。
但是,計算兩時間點相差的天數又比較簡單,直接用datediff函式即可。
以下為SQL示例:
但是,計算兩時間點相差的天數又比較簡單,直接用datediff函式即可。
以下為SQL示例:
--1)4月29日0:00~5月1日23:59期間,單次直播“迷你世界”遊戲超過4小時的主播。 with tab_live_real as( select room_id,name,is_profession,switch_time,updated_time,cast(updated_time as timestamp)-cast(switch_time as timestamp) time_minus from xxx_live_history_status where switch_time between '2017-04-29 00:00:00' and '2017-05-01 23:59:59' and game_id=1269) select a1.room_id,a2.creator_uid,a1.is_profession,a1.live_duration from ( select room_id,is_profession,switch_time,updated_time,cast(hour(time_minus)*60+minute(time_minus)+second(time_minus)/60 as bigint) live_duration from tab_live_real where cast(hour(time_minus)*60+minute(time_minus)+second(time_minus)/60 as bigint) >= 240) a1 left join xxx_room a2 on a1.room_id=a2.id; --2)4月29日0:00~5月1日23:59期間,累計直播“迷你世界”遊戲超過10小時的主播。 with tab_live_real as( select room_id,name,is_profession,switch_time,updated_time,cast(updated_time as timestamp)-cast(switch_time as timestamp) time_minus from xxx_live_history_status where switch_time between '2017-04-29 00:00:00' and '2017-05-01 23:59:59' and game_id=1269) select a1.room_id,a2.creator_uid,a1.live_duration from ( select room_id,cast(sum(hour(time_minus)*60+minute(time_minus)+second(time_minus)/60) as bigint) live_duration from tab_live_real group by room_id having cast(sum(hour(time_minus)*60+minute(time_minus)+second(time_minus)/60) as bigint)>=600) a1 left join xxx_room a2 on a1.room_id=a2.id;