- 注册
- 2012-08-11
- 消息
- 7,346
- 荣誉分数
- 1,190
- 声望点数
- 273
不知你有没有注意到,你有一个错误。假如某人2年内正好出境4次, 而且满足任意2次之间的天数大于30天,你的X对于此人返回4条记录,但是第4条记录的days_diff是0。于是只有3条记录满足days_diff>30的条件,此人就被过滤掉了。不知你明白否。
谢谢,指出一个BUG。现在差不多了吧??
select y.pid as person_id, y.sdate as first_qualified_date
---from
------(select x.pid, x.sdate, row_number() over (partition by x.pid order by x.sdate) as rak, count(pid) over (partition by x.pid) as cnt
------from
---------(select pid, sdate, lead(sdate,1,sdate+31) over (partition by pid order by sdate)-sdate as days_diff
------------from t
------------where sdate>sysdate-730
---------) x
------where days_diff>30
---) y
---where y.rak=1 and y.cnt>=4;
谢谢,指出一个BUG。现在差不多了吧??
select y.pid as person_id, y.sdate as first_qualified_date
---from
------(select x.pid, x.sdate, row_number() over (partition by x.pid order by x.sdate) as rak, count(pid) over (partition by x.pid) as cnt
------from
---------(select pid, sdate, lead(sdate,1,sdate+31) over (partition by pid order by sdate)-sdate as days_diff
------------from t
------------where sdate>sysdate-730
---------) x
------where days_diff>30
---) y
---where y.rak=1 and y.cnt>=4;
其实把最后一行where中的4改成3即可。想想为什么![]()