2

我需要获得连续未付款的金额:

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

4

1 回答 1

0

这不是您问题的完整答案,但它是获取每个 ID 的未付款项数量的可能解决方案。它为Not_paid状态分配值 1,为Paid状态分配 -1。因此,我们可以按 ID 对查询进行分组,并对 value 列求和以获得未付款项的数量。对于负数的总和,我们分配为零,因为它们没有未付款项。

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,
       DECODE(SIGN(SUM(value)), -1, 0, SUM(value))
  FROM (SELECT id,
               dater,
               status,
               DECODE(status, 'Paid', -1, 1) value
          FROM payments
       )
 GROUP BY id
 ORDER BY id;

现在,此查询适用于您示例中的数据集,但可能不适用于更大的数据集。此外,如果 Not_paid 状态的付费状态数量不相等,它也不会起作用。例如,在您的ID = 2示例中,如果帐户在 5 月份全额支付,则需要将 2 个付费条目加载到您的表中才能使我的解决方案起作用。如果只加载了 1 个付费条目,那么我的解决方案仍会显示此 ID 所需的未付款项。

于 2013-06-27T18:18:33.247 回答