日期/时间范围内的模式序列

时间:2019-04-10 05:23:05

标签: sql hive

我有一个问题,我需要在..

上的帮助

在下面的示例中,如果我要基于ID中的数据模式010(作为方案1),000(作为方案2),111(作为方案3)获取方案。请忽略不遵循该模式的记录。

例如:

id  date        Status
1   2012-10-18  1
1   2012-10-19  1
1   2012-10-20  0
1   2012-10-21  0
1   2012-10-22  0
1   2012-10-23  0
1   2012-10-24  1
1   2012-10-25  0
1   2012-10-26  0
1   2012-10-27  0
1   2012-10-28  1
2   2012-10-19  0
2   2012-10-20  0
2   2012-10-21  0
2   2012-10-22  1
2   2012-10-23  1

scenario1:
1   2012-10-23  0
1   2012-10-24  1
1   2012-10-25  0

Scenario2:
1   2012-10-20  0
1   2012-10-21  0
1   2012-10-22  0
2   2012-10-19  0
2   2012-10-20  0
2   2012-10-21  0

Scenario3 - none (no records)

1 个答案:

答案 0 :(得分:0)

您可以将模式构造为字符串,然后使用字符串比较。

至少有部分技巧是要让模式中的所有行都存在,因此您需要构造所有可能出现每行的模式:

select t.*
from (select t.*,
             concat(lag(status), -2) over (partition by id order by date),
                    lag(status), -1) over (partition by id order by date),
                    status
                   ) as pat1,
             concat(lag(status), -1) over (partition by id order by date),
                    status,
                    lead(status), 1) over (partition by id order by date)
                   ) as pat2,
             concat(status,
                    lead(status), 1) over (partition by id order by date),
                    lead(status), 2) over (partition by id order by date)
                   ) as pat3
      from t
     ) t
where '010' in (pat1, pat2, pat3);