正经问题: SQL 语言

不知你有没有注意到,你有一个错误。假如某人2年内正好出境4次, 而且满足任意2次之间的天数大于30天,你的X对于此人返回4条记录,但是第4条记录的days_diff是0。于是只有3条记录满足days_diff>30的条件,此人就被过滤掉了。不知你明白否。
你的程序好像也不对。假设一个人有5条出境记录,按顺序1,3,4,5的日期间隔正好是30天,但是由于第2条记录的存在,这个人显然不能入选。但是你的where语句正好把第二条记录给滤掉了,这个人就错误地入选了。这个理解对吗?
 
学习了以上两位对lead和lag分析统计函数的使用,受益非浅:cool:

论坛里要是开设一个专门讨论一些感兴趣的技术问题的板块就好了,既然咱们村大部分人都是做IT的,可以促进相互之间的学习和交流。

你们上班码,回家、周末继续码?:rolleyes:
 
不知你有没有注意到,你有一个错误。假如某人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;

这是什么SQL?T-SQL or P/L-SQL?
 
谢谢,指出一个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即可。想想为什么:confused:
 
你的程序好像也不对。假设一个人有5条出境记录,按顺序1,3,4,5的日期间隔正好是30天,但是由于第2条记录的存在,这个人显然不能入选。但是你的where语句正好把第二条记录给滤掉了,这个人就错误地入选了。这个理解对吗?

如果你真想理解的话,建议你学一学Lead函数,一点都不难。
 
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即可。想想为什么:confused:

我这样改没错吧。我刚发帖就想到了另一改法。当初以为楼主要求4次出境都要30天,没看清题目
 
哪个是何叔的马甲呢? 闭着眼睛抡一竿子:蜜婆思。。。这名字起的有90年代初的哀啼文化。
 
你的程序好像也不对。假设一个人有5条出境记录,按顺序1,3,4,5的日期间隔正好是30天,但是由于第2条记录的存在,这个人显然不能入选。但是你的where语句正好把第二条记录给滤掉了,这个人就错误地入选了。这个理解对吗?

你说的这种情况,用MIPS的CODE,这个人的记录不会录选, 但是这记录显然应该录选的。 因为MIPS的CODE只选连续4次出境超过30 天的, 而第2条记录造成不满足连续4次。
 
后退
顶部