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 Go to next message
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 #631964 is a reply to message #631962] Thu, 22 January 2015 07:26 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
And the query that your tried is?
Re: Distributing a total amount (payment) according to individual records (requests) [message #631966 is a reply to message #631964] Thu, 22 January 2015 07:47 Go to previous messageGo to next message
OraFerro
Messages: 433
Registered: July 2011
Senior Member
Hi LK,

In fact I could not start a query, just an incomplete stored procedure (which is not what I want) that:
1- calculates the total payments
2- calculate the number of requests not fully paid
3- list the fully paid requests
4- put the difference between total requests and total payments as the paid amount for the first unpaid request and mark it as unpaid or partially paid (if the difference is 0 or more).

But I did not continue the SP as I need a query, give me hints and I will try (currently reading about MODEL clause)

Thanks,
Ferro
Re: Distributing a total amount (payment) according to individual records (requests) [message #631968 is a reply to message #631962] Thu, 22 January 2015 07:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I'm not sure I understand your output not what it should for the COMPLETE test case.
For instance, "request_id" is all the same but "request_date" changes why? Where do come the other values? Why?
Where do come each "paid_amount"? Why?
What is "status? How is it define?
What should result be with the other requests?
Why are they not in the result?


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 Go to previous message
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.
Previous Topic: How to trim the last matching string using regexp_substr
Next Topic: bulk collect
Goto Forum:
  


Current Time: Thu Apr 25 12:30:45 CDT 2024