简体   繁体   English

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

[英]Sequence of Patterns within Date/time range

I have a problem I would need help on .. 我有一个问题,我需要帮助..

In the example below, if I want to get scenarios based on the data patterns 010 as scenario1, 000 as scenario2, 111 as scenario3 within the Id.. Ignore the records that doesn't follow the pattern.. 在下面的示例中,如果我想基于数据模式010获取方案作为scenario1,000作为scenario2,111作为Id中的scenario3 ..忽略不遵循模式的记录..

Ex: 例如:

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)

You can construct the patterns as strings and then use string comparison. 您可以将模式构造为字符串,然后使用字符串比较。

At least part of the trick is that you want all rows in the pattern, so you need to construct all potential patterns where each row might appear: 至少部分技巧是你想要模式中的所有行,所以你需要构造每行可能出现的所有潜在模式:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM