如何编写hql / sql来解决下面的问题

问题描述 投票:0回答:1

我想写一个下面的代码来实现输出?

输入:

+----------------------+
|         dttm         |
+----------------------+
| 2014-11-18 16:23:01  |
| 2014-11-18 16:23:02  |
| 2014-11-18 16:26:14  |
| 2014-11-18 16:26:15  |
| 2014-11-18 16:26:16  |
| 2014-11-18 17:43:02  |
| 2014-11-18 17:43:03  |
| 2018-12-17 23:59:59  |
| 2018-12-18 00:00:00  |
| 2019-01-17 00:00:00  |
| 2019-01-17 00:00:01  |
+----------------------+

输出:

+----------------------+
|         dttm         |
+----------------------+
| 2014-11-18 16:23:01  |
| 2014-11-18 16:23:02  |
| 2014-11-18 16:26:14  |
| 2014-11-18 16:26:15  |
| 2014-11-18 16:26:16  |
| 2014-11-18 16:26:16  |
| 2014-11-18 17:43:02  |
| 2014-11-18 17:43:03  |
| 2018-12-17 23:59:59  |
| 2018-12-18 00:00:00  |
| 2019-01-17 00:00:00  |
| 2019-01-17 00:00:01  |
+----------------------+

实现逻辑:第1行和第2行应该有1秒的差异,3和4应该有1秒的差异,依此类推。

如果不是这种情况,请复制该订单项。

sql hive hql hiveql
1个回答
0
投票

试试这个



select a.dttm from (
select   t1.rn, t1.dttm, case when  (unix_timestamp(t2.dttm) - unix_timestamp(t1.dttm) )  = 1 then '1' when  (unix_timestamp(t2.dttm) - unix_timestamp(t1.dttm)) is null then '1' else '1,2' end as split_rec
from (select dttm, row_number () over () as rn from tmstmp) t1 left outer join
(select dttm, row_number () over () as rn from tmstmp) t2
on t1.rn = t2.rn-1 and pmod(t2.rn,2) =0) a
lateral view explode(split(a.split_rec , ',')) temp as td1
;



Output is :

+-----------+------------------------+--+
| temp.td1  |         a.dttm         |
+-----------+------------------------+--+
| 1         | 2014-11-18 16:23:01.0  |
| 1         | 2014-11-18 16:23:02.0  |
| 1         | 2019-01-17 00:00:00.0  |
| 1         | 2019-01-17 00:00:01.0  |
| 1         | 2014-11-18 16:26:14.0  |
| 1         | 2014-11-18 16:26:15.0  |
| 1         | 2014-11-18 16:26:16.0  |
| 2         | 2014-11-18 16:26:16.0  |
| 1         | 2014-11-18 16:26:16.0  |
| 1         | 2014-11-18 17:43:02.0  |
| 1         | 2014-11-18 17:43:03.0  |
| 1         | 2018-12-17 23:59:59.0  |
| 1         | 2018-12-18 00:00:00.0  |
+-----------+------------------------+--+


© www.soinside.com 2019 - 2024. All rights reserved.