问题:在hive中,A表中有一个时间的字段,类型位string,格式为2018-12-0 13:34:12;在B表中有字段start_time和end_time,类型为string,格式为2018-12-06 13:34:12,先需要将两表按id关联后新增一个标记字段(当A表的时间在B表的start_time和end_time之间就标记为1,不在区间内就标记为0),B表中还有一个订单id,string类型

解决方法:使用join和case when then和lead窗口函数
先在hive中创建两张表并插入模拟数据:

-- 创建表
create table A (
plate_num string,
time string
);
create table B (
order_id string,
plate_num string,
start_time string,
end_time string
);-- 插入模拟数据
insert into A values('泸A88888', '2018-04-02 09:34:12');
insert into A values('泸A66666', '2018-04-02 12:34:12');
insert into A values('泸A00000', '2018-04-02 10:34:12');
insert into A values('泸A22222', '2018-04-02 15:34:12');
insert into B values('201812060001', '泸A88888', '2018-04-02 09:00:12', '2018-04-02 10:00:12');
insert into B values('201812060002', '泸A66666', '2018-04-02 09:10:12', '2018-04-02 10:00:12');
insert into B values('201812060003', '泸A99999', '2018-04-02 09:15:12', '2018-04-02 09:55:12');
insert into B values('201812060004', '泸A88888', '2018-04-02 10:05:12', '2018-04-02 10:30:12');
insert into B values('201812060005', '泸A22222', '2018-04-02 10:10:12', '2018-04-02 10:44:12');
insert into B values('201812060006', '泸A99999', '2018-04-02 10:15:12', '2018-04-02 10:34:12');
insert into B values('201812060007', '泸A66666', '2018-04-02 10:20:12', '2018-04-02 10:30:12');
insert into B values('201812060008', '泸A66666', '2018-04-02 10:35:12', '2018-04-02 10:40:12');
insert into B values('201812060009', '泸A00000', '2018-04-02 10:35:12', '2018-04-02 10:45:12');
insert into B values('201812060010', '泸A99999', '2018-04-02 10:36:12', '2018-04-02 10:45:12');
insert into B values('201812060011', '泸A22222', '2018-04-02 10:50:12', '2018-04-02 11:20:12');

使用case when then 求表A的时间是否在表B的开始和结束时间之间,在标记为1,不在标记为0

-- 查找结果
-- 方法一:不将时间字段转换为时间戳
select
A.plate_num plate_num,
A.time time,
B.start_time start_time,
B.end_time end_time,
case when unix_timestamp(A.time) between unix_timestamp(B.start_time) and unix_timestamp(B.end_time) then 1 else 0 end sign
from
A
join
B
on
A.plate_num = B.plate_num;-- 结果:
OK
泸A88888 2018-04-02 09:34:12 2018-04-02 09:00:12 2018-04-02 10:00:12 1
泸A22222 2018-04-02 15:34:12 2018-04-02 10:50:12 2018-04-02 11:20:12 0
泸A88888 2018-04-02 09:34:12 2018-04-02 10:05:12 2018-04-02 10:30:12 0
泸A22222 2018-04-02 15:34:12 2018-04-02 10:10:12 2018-04-02 10:44:12 0
泸A00000 2018-04-02 10:34:12 2018-04-02 10:35:12 2018-04-02 10:45:12 0
泸A66666 2018-04-02 12:34:12 2018-04-02 09:10:12 2018-04-02 10:00:12 0
泸A66666 2018-04-02 12:34:12 2018-04-02 10:20:12 2018-04-02 10:30:12 0
泸A66666 2018-04-02 12:34:12 2018-04-02 10:35:12 2018-04-02 10:40:12 0
Time taken: 9.907 seconds, Fetched: 8 row(s)-- 方法二:将时间字段转换为时间戳
select
A.plate_num plate_num,
A.time time,
B.start_time start_time,
B.end_time end_time,
case when A.time between B.start_time and B.end_time then 1 else 0 end sign
from
A
join
B
on
A.plate_num = B.plate_num;-- 结果:
OK
泸A88888 2018-04-02 09:34:12 2018-04-02 09:00:12 2018-04-02 10:00:12 1
泸A22222 2018-04-02 15:34:12 2018-04-02 10:50:12 2018-04-02 11:20:12 0
泸A88888 2018-04-02 09:34:12 2018-04-02 10:05:12 2018-04-02 10:30:12 0
泸A22222 2018-04-02 15:34:12 2018-04-02 10:10:12 2018-04-02 10:44:12 0
泸A00000 2018-04-02 10:34:12 2018-04-02 10:35:12 2018-04-02 10:45:12 0
泸A66666 2018-04-02 12:34:12 2018-04-02 09:10:12 2018-04-02 10:00:12 0
泸A66666 2018-04-02 12:34:12 2018-04-02 10:20:12 2018-04-02 10:30:12 0
泸A66666 2018-04-02 12:34:12 2018-04-02 10:35:12 2018-04-02 10:40:12 0
Time taken: 0.985 seconds, Fetched: 8 row(s)

求订单中间的休息时间,使用lead的窗口函数

select
t.order_id, t.plate_num, t.start_time, t.end_time,
(unix_timestamp(t.start_time1) - unix_timestamp(t.end_time)) as interval_time
from(
select
*,
-- 根据车牌号分区,开始时间升序取后一个订单的开始时间
lead(start_time, 1, 0) over(partition by plate_num order by start_time) start_time1
from b) t;-- 结果
OK
201812060009    泸A00000 2018-04-02 10:35:12 2018-04-02 10:45:12 NULL
201812060005    泸A22222 2018-04-02 10:10:12 2018-04-02 10:44:12 360
201812060011    泸A22222 2018-04-02 10:50:12 2018-04-02 11:20:12 NULL
201812060002    泸A66666 2018-04-02 09:10:12 2018-04-02 10:00:12 1200
201812060007    泸A66666 2018-04-02 10:20:12 2018-04-02 10:30:12 300
201812060008    泸A66666 2018-04-02 10:35:12 2018-04-02 10:40:12 NULL
201812060001    泸A88888 2018-04-02 09:00:12 2018-04-02 10:00:12 300
201812060004    泸A88888 2018-04-02 10:05:12 2018-04-02 10:30:12 NULL
201812060003    泸A99999 2018-04-02 09:15:12 2018-04-02 09:55:12 1200
201812060006    泸A99999 2018-04-02 10:15:12 2018-04-02 10:34:12 120
201812060010    泸A99999 2018-04-02 10:36:12 2018-04-02 10:45:12 NULL
Time taken: 9.289 seconds, Fetched: 11 row(s)

hive中判断A表时间字段是否在B表的两个时间字段中及求订单中间休息时间相关推荐

  1. 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解...

    salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解 建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema ...

  2. salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

    建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...

  3. hive建立内部表映射hbase_快手 HBase 在千亿级用户特征数据分析中的应用与实践...

    分享嘉宾:陈杨 快手 编辑整理:Hoh Xil 内容来源:BigData NoSQL 12th Meetup 出品社区:DataFun 注:欢迎转载,转载请注明出处. 快手建设 HBase 差不多有2 ...

  4. Oracle存储过程中如何根据指定的参数判断该参数的值否存在数据表中:

    摘要:最近项目中用到了Oracle存储过程,所以就自己尝试着写了下,下面我把我遇到的问题描述一下:就是在我处理解析Clob字段中的xml字符串的时候,有个需求就是根据指定的主键参数,来判断该参数的值是 ...

  5. js判断json有没有某值_JS中判断JSON数据是否存在某字段的方法 JavaScript中判断json中是否有某个字段...

    方式一 !("key" in obj) 方式二 obj.hasOwnProperty("key")  //obj为json对象. 实例: var jsonwor ...

  6. Sql Server中判断表或者数据库是否存在

    SQL Server中判断数据库是否存在: 法(一): select * From master.dbo.sysdatabases where name='数据库名' 法(二): if db_id(' ...

  7. hive 指定字段插入数据_Hive 表之间数据处理,Int 类型字段部分字段出现 NULL情况...

    背景 hive 中有一张待处理的分区表,存储的方式是parquet,处理之后的目标表是一张非分区的外部表,并且分隔方式为 ",". 问题 部分记录的 int 类型字段 出现 nul ...

  8. mysql从表截取信息_mysql中循环截取用户信息并插入到目标表对应的字段中

    操作环境:有表game_list,字段:uid,score1,score2,seat_id,last_update: 传入参数为i_player_detail ,传入的值为多个用户的id.之前分数.之 ...

  9. 查询oracle 数据库中回滚段中一个时间点被修改的表数据并还原表中原来数据

    利用下面的SQL就可以查处最近更改的数据. SQL> SELECT ID,NAME,VERSIONS_STARTTIME,VERSIONS_ENDTIME,VERSIONS_OPERATION ...

最新文章

  1. at24c16如何划分出多个读写区_51单片机向at24c16EPROM写入一个数据每问题,写入多个数据,读出的数据都一样...
  2. [LeetCode] Binary Tree Postorder题解
  3. Boost:bimap双图lambda表达式的测试程序
  4. 28 | 案例篇:一个SQL查询要15秒,这是怎么回事?
  5. 22-React JSX语法
  6. c语言的有趣小程序,一个有趣的小程序
  7. C语言实现的简单的线程池
  8. golang web php,golang 适合做web开发吗
  9. 关于结构体的浅拷贝和深拷贝
  10. 计算机网络之JSONP跨域
  11. 【NLP专栏】图解 BERT 预训练模型!
  12. 第十二课:实验二 循环链表实验
  13. linux redis命令客户端,Redis客户端
  14. 微软更新补丁手动下载地址
  15. HTC T328W刷机包 仿三星S5 UI美化 精简 S5下拉
  16. python 解决Fatal error in launcher:错误问题
  17. 摄像头 SONY VISCA 协议
  18. 区块链技术及应用概述
  19. python贝叶斯算法的论文_3个范例带你读懂贝叶斯法则
  20. 研发人员专用表情包上架了

热门文章

  1. 笔记本电池修复软件 v2.0 绿色版
  2. 使用grub实现虚拟软盘
  3. UDS-如何在CAPL中实现读取DTC和它的状态
  4. linux下各种上网代理配置总结
  5. [计算机故障]华为手机无法连接XP系统(inf中找不到所需的段落)
  6. 数美科技:智能时代怎么样构建金融反欺诈体系
  7. APP的注册和登录功能设计
  8. assa_【脚本教程】ASSA指令解析与案例
  9. 睿智的目标检测33——Keras搭建Efficientdet目标检测平台
  10. Jain's fairness index