查询将付款与余额进行比较

时间:2013-05-30 17:20:18

标签: oracle

有一个处理客户付款和计算剩余余额的申请。比如说,客户的余额为910.09美元。登录到应用程序的代理程序向客户应用15.45美元。现在,余额为894.64美元。

之后,代理商再次支付33.65美元。余额现在是894.64 - 33.65 = 860.99。

嗯,有一个继承的流程(比如,流程X),每次应用付款时,它会填充我的表格,其中包含自开始以来每次处理时应用的所有付款。

CREATE TABLE CUSTOMER_PAYMENTS
(
  CUSTOMER_NO   NUMBER,
  PAYMENT_DATE  DATE,
  PAYMENT_AMT   NUMBER(20,4),
  REM_BALANCE   NUMBER(20,4)
)
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;
SET DEFINE OFF;
Insert into CUSTOMER_PAYMENTS
   (CUSTOMER_NO, PAYMENT_DATE, PAYMENT_AMT, REM_BALANCE)
 Values
   (4926, TO_DATE('05/30/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), -15.45, 894.64);
Insert into CUSTOMER_PAYMENTS
   (CUSTOMER_NO, PAYMENT_DATE, PAYMENT_AMT, REM_BALANCE)
 Values
   (4926, TO_DATE('05/30/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), -15.45, 860.99);
Insert into CUSTOMER_PAYMENTS
   (CUSTOMER_NO, PAYMENT_DATE, PAYMENT_AMT, REM_BALANCE)
 Values
  (4926, TO_DATE('05/30/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), -33.65, 860.99);
COMMIT;

在Process X运行两次后,这就是我的表。现在,我面临的挑战是确定独特的付款方式。如何比较付款后的剩余余额,看看它是否真的是那个号码,如果不是,我会说这是重复的?

在这种情况下,第3行894.64 - 15.45!= 860.99。因此,第2行15.45是重复的。

提前致谢。

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,我认为这不能得到可靠回答,因为无法确定订购。如果假设剩余的余额应该减少,这可能会给你一些重复的东西:

SELECT customer_no, payment_date, payment_amt, rem_balance, prev_bal
FROM (
    SELECT customer_no, payment_date, payment_amt, rem_balance,     
    LAG(rem_balance, 1,  0) OVER   
    (PARTITION BY customer_no ORDER BY rem_balance DESC) prev_bal,
    rownum r
    FROM customer_payments
    ORDER BY rem_balance DESC
)
WHERE r > 1 and
prev_bal + payment_amt <> rem_balance
ORDER BY customer_no, rem_balance DESC

[SQL Fiddle在这里。]

[我在这里做了很多假设 - 就像rem_balance一直在减少,从来没有通常是999999999999等等]