Home » SQL & PL/SQL » SQL & PL/SQL » Get all installments based on several plans (get unique values based on a single column) (11.2.0.10)
Get all installments based on several plans (get unique values based on a single column) [message #649205] Thu, 17 March 2016 05:45 Go to next message
OraFerro
Messages: 433
Registered: July 2011
Senior Member
Hi All,

I have a table that stores all installments and their plan. In case of re-scheduling, only re-scheduled installments are moved to the new plan. I need a list of all installements but in case an installement is repeated in more than one plan, get the installment that belongs to the last plan.

The case is as follows:
CREATE table try_plan_inst 
(
  fk_plan_id NUMBER,
  inst_id NUMBER,
  inst_date DATE,
  inst_amount number
);

INSERT ALL 
  INTO TRY_PLAN_INST VALUES (1,1,TO_DATE('01-01-2000','DD-MM-YYYY'),1000)
  INTO TRY_PLAN_INST VALUES (1,2,TO_DATE('01-01-2001','DD-MM-YYYY'),1000)
  INTO TRY_PLAN_INST VALUES (1,3,TO_DATE('01-01-2002','DD-MM-YYYY'),1000)
  INTO TRY_PLAN_INST VALUES (1,4,TO_DATE('01-01-2003','DD-MM-YYYY'),1000)
  INTO TRY_PLAN_INST VALUES (1,5,TO_DATE('01-01-2004','DD-MM-YYYY'),1000)
  INTO TRY_PLAN_INST VALUES (1,6,TO_DATE('01-01-2005','DD-MM-YYYY'),1000)
  INTO TRY_PLAN_INST VALUES (2,4,TO_DATE('01-01-2006','DD-MM-YYYY'),1500)
  INTO TRY_PLAN_INST VALUES (2,5,TO_DATE('01-01-2007','DD-MM-YYYY'),1500)
  INTO TRY_PLAN_INST VALUES (2,6,TO_DATE('01-01-2008','DD-MM-YYYY'),1500)
  INTO TRY_PLAN_INST VALUES (3,6,TO_DATE('01-01-2009','DD-MM-YYYY'),1400)
SELECT * FROM DUAL;

--desired output: all installements but in case an installement is repeated in more than one plan, get the installment that belongs to the last plan
fk_plan_id inst_id inst_date  inst_amount
1          1       01-01-2000 1000
1          2       01-01-2001 1000
1          3       01-01-2002 1000
2          4       01-01-2006 1500
2          5       01-01-2007 1500
3          6       01-01-2009 1400




Many thanks,
Ferro
Re: Get all installments based on several plans (get unique values based on a single column) [message #649208 is a reply to message #649205] Thu, 17 March 2016 06:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What does mean "last plan"? Highest plan_id or latest inst_date?
Once you clear this point just have a look at Have a look at ROW_NUMBER, RANK and DENSE_RANK functions and at http://www.orafaq.com/forum/mv/msg/160920/472554/102589/#msg_472554 post.

Re: Get all installments based on several plans (get unique values based on a single column) [message #649212 is a reply to message #649205] Thu, 17 March 2016 13:40 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl> SELECT fk_plan_id, inst_id, inst_date, inst_amount
  2  FROM   (SELECT fk_plan_id, inst_id, inst_date, inst_amount,
  3  		    DENSE_RANK () OVER (PARTITION BY inst_id ORDER BY fk_plan_id DESC) dr
  4  	     FROM   try_plan_inst)
  5  WHERE  dr = 1
  6  ORDER  BY fk_plan_id, inst_id
  7  /

FK_PLAN_ID    INST_ID INST_DATE   INST_AMOUNT
---------- ---------- ----------- -----------
         1          1 01-JAN-2000        1000
         1          2 01-JAN-2001        1000
         1          3 01-JAN-2002        1000
         2          4 01-JAN-2006        1500
         2          5 01-JAN-2007        1500
         3          6 01-JAN-2009        1400

6 rows selected.

Re: Get all installments based on several plans (get unique values based on a single column) [message #649213 is a reply to message #649212] Thu, 17 March 2016 13:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Tell me and I'll forget; show me and I may remember; involve me and I'll understand.

Re: Get all installments based on several plans (get unique values based on a single column) [message #649259 is a reply to message #649213] Sat, 19 March 2016 23:17 Go to previous message
OraFerro
Messages: 433
Registered: July 2011
Senior Member


Dear Barbara,
Lots of thanks.

Dear Michel,
I totally agree, thanks a lot.
Previous Topic: Inserting data into local partitioned index vs global partitioned index
Next Topic: Help with Function
Goto Forum:
  


Current Time: Thu Apr 25 18:50:44 CDT 2024