我需要获得连续未付款的金额:
with payments as
(
select '1' as ID, '20130331' as DateR, 'Not_paid' as Status from dual
union
select '1' as ID, '20130430' as DateR, 'Paid' as Status from dual
union
select '1' as ID, '20130531' as DateR, 'Not_paid' as Status from dual
union
select '2' as ID, '20130331' as DateR, 'Not_paid' as Status from dual
union
select '2' as ID, '20130430' as DateR, 'Not_paid' as Status from dual
union
select '3' as ID, '20130331' as DateR, 'Paid' as Status from dual
union
select '3' as ID, '20130430' as DateR, 'Paid' as Status from dual
union
select '3' as ID, '20130531' as DateR, 'Paid' as Status from dual
)
select ID, dater, status, dense_rank() over (partition by ID, status order by dater asc) rnk from payments
正如你从这里看到的,我从 id 2 中得到了正确的未付款数:他的第一次未付款是在 3 月,第二次是在 4 月。ID 3 也可以,因为我稍后会将他排除在外,但对于 ID 1,它表示第二次未付款是在 5 月,而我想成为第一次,因为他在 3 月未付款,但在 4 月再次付款,所以它应该从那里开始排名。一旦他支付了最后一笔款项,流程就会重新开始。
这个想法是在没有复杂查询的情况下保持简单。我只需要做与密集排名相同的事情,但前提是日期是连续的
我希望这个例子足够清楚。
编辑:这是我从当前查询中得到的:
ID DATER STATUS RNK
1 20130331 Not_paid 1
1 20130531 Not_paid 2
1 20130430 Paid 1
2 20130331 Not_paid 1
2 20130430 Not_paid 2
3 20130331 Paid 1
3 20130430 Paid 2
3 20130531 Paid 3
我想得到的是:
ID DATER STATUS RNK
1 20130331 Not_paid 1
1 20130430 Paid 1
1 20130531 Not_Paid 1
2 20130331 Not_paid 1
2 20130430 Not_paid 2
3 20130331 Paid 1
3 20130430 Paid 2
3 20130531 Paid 3
这样,如果我想获得最大(排名)来检查用户当前有多少未付款,我会得到该 ID 有 1 次未付款,ID 2 有两个连续未付款,ID 3 有 0 次未付款。这是因为在第四次连续未付款时,我必须将用户视为流失。
编辑:29/06/2013
在另一个论坛有人给了我一个完美的解决方案: https ://forums.oracle.com/thread/2555552