Home » SQL & PL/SQL » SQL & PL/SQL » Distributing a total amount (payment) according to individual records (requests) (11.2.0.1.0)
Distributing a total amount (payment) according to individual records (requests) [message #631962] |
Thu, 22 January 2015 06:34 |
|
OraFerro
Messages: 433 Registered: July 2011
|
Senior Member |
|
|
Hi All,
I have payment requests table (request) and payments table (payment). Each payment is done after receiving a request but has no link to the request that asked for the payment. Payments can be done in one installment or many but cant exceed the requested amount.
I need to list the requests and distribute the total amount of payments on each request sequentially and indicate whether the last request was not paid, fully paid, or partially paid.
I have the following sample:
CREATE TABLE REQUEST
(
REQ_ID NUMBER PRIMARY KEY,
REQ_DATE DATE,
REQ_AMOUNT NUMBER
);
CREATE TABLE PAYMENT
(
PAYMNET_ID NUMBER PRIMARY KEY,
PAYMENT_DATE DATE,
PAYMENT_AMOUNT NUMBER
);
INSERT ALL
INTO REQUEST VALUES (1,TO_DATE('01-01-2010','DD-MM-YYYY'),100000)
INTO REQUEST VALUES (2,TO_DATE('01-02-2010','DD-MM-YYYY'),100000)
INTO REQUEST VALUES (3,TO_DATE('01-03-2010','DD-MM-YYYY'),100000)
INTO REQUEST VALUES (4,TO_DATE('01-04-2010','DD-MM-YYYY'),100000)
INTO PAYMENT VALUES (1,TO_DATE('05-01-2010','DD-MM-YYYY'),100000)
INTO PAYMENT VALUES (2,TO_DATE('05-02-2010','DD-MM-YYYY'),100000)
INTO PAYMENT VALUES (3,TO_DATE('05-03-2010','DD-MM-YYYY'),50000)
INTO PAYMENT VALUES (4,TO_DATE('16-03-2010','DD-MM-YYYY'),40000)
INTO PAYMENT VALUES (5,TO_DATE('25-03-2010','DD-MM-YYYY'),10000)
INTO PAYMENT VALUES (6,TO_DATE('05-04-2010','DD-MM-YYYY'),33000)
SELECT * FROM DUAL;
What I am trying to query is the following:
request_id, request_amount, request_date, paid_amount, status(not paid/partially paid/fully paid)
1 100000 01-01-2010 100000 fully paid
1 100000 01-02-2010 100000 fully paid
1 100000 01-03-2010 100000 fully paid
1 100000 01-04-2010 33000 partially paid
Thanks,
Ferro
[Updated on: Thu, 22 January 2015 06:36] Report message to a moderator
|
|
|
|
|
|
Re: Distributing a total amount (payment) according to individual records (requests) [message #631971 is a reply to message #631962] |
Thu, 22 January 2015 08:23 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
with t as (
select sum(payment_amount) total_payment
from payment
)
select req_amount,
req_date,
case sign(total_payment - sum(req_amount) over(order by req_date))
when -1 then 'partially paid'
else 'fully paid'
end status
from request,
t
order by req_date
/
REQ_AMOUNT REQ_DATE STATUS
---------- --------- --------------
100000 01-JAN-10 fully paid
100000 01-FEB-10 fully paid
100000 01-MAR-10 fully paid
100000 01-APR-10 partially paid
SQL>
SY.
|
|
|
Goto Forum:
Current Time: Thu Apr 25 12:30:45 CDT 2024
|