Home » SQL & PL/SQL » SQL & PL/SQL » Instance Sequence Priority Values (Oracle 11g)
Instance Sequence Priority Values [message #610089] Mon, 17 March 2014 09:00 Go to next message
developer12
Messages: 88
Registered: July 2013
Member
I have a list of sequences defined in Table A as:
seq instance
1 66
2 5
3 77
4 56
Now I have another Table B :
instance Value_a Value_b
77 10001 10002
88 10001 10001
4 10003 10004
Now I need to join Table A and Table B based on the fact that i have to look thru Table B and find the first records which have Value_a and Value_b.
i.e.: Loop thru the Table B and find the instance which has atleast 1 record in instance sequence(1,2,3,4)
So, first for 66 there are no records, we move on to 5 which also does not have any records in Table B, so we move on to 77 and find a record in Table B. We have to report that and display results:
Instance Value_a Value_b
77 10001 10002
Re: Instance Sequence Priority Values [message #610090 is a reply to message #610089] Mon, 17 March 2014 09:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It would be helpful if you provided DDL (CREATE TABLE ...) for tables involved.
It would be helpful if you provided DML (INSERT INTO ...) for test data.
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/311
It would be helpful if you provided expected/desired results & a detailed explanation how & why the test data gets transformed or organized.
Re: Instance Sequence Priority Values [message #610091 is a reply to message #610089] Mon, 17 March 2014 09:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Is this not a join between the 2 tables or a select from table B where instance exists in table A?

Re: Instance Sequence Priority Values [message #610093 is a reply to message #610091] Mon, 17 March 2014 09:23 Go to previous messageGo to next message
developer12
Messages: 88
Registered: July 2013
Member
Yes it is a join and select from Table B but we have to choose only the first record for the instances listed in Table A in the order of 1,2,3,4 whichever is found first.
Re: Instance Sequence Priority Values [message #610094 is a reply to message #610093] Mon, 17 March 2014 09:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

How to you define "first" (from the data ONLY)?

Re: Instance Sequence Priority Values [message #610095 is a reply to message #610093] Mon, 17 March 2014 09:32 Go to previous messageGo to next message
developer12
Messages: 88
Registered: July 2013
Member
Like if there is record present in Table B for instance 5 as well as 77 instance, we choose only the record in Table B for instance 5 as for that sequence is 2 in table A and comes before.
Re: Instance Sequence Priority Values [message #610096 is a reply to message #610095] Mon, 17 March 2014 09:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK, can you now provided the test case as BlackSwan said.

Re: Instance Sequence Priority Values [message #610100 is a reply to message #610096] Mon, 17 March 2014 10:49 Go to previous messageGo to next message
developer12
Messages: 88
Registered: July 2013
Member
Here is how the DDL, DML and the results I need:
CREATE TABLE Table_a
(
  SEQ      NUMBER,
  INSTANCE  NUMBER
)
/

SET DEFINE OFF;
Insert into Table_a
   (SEQ, INSTANCE)
 Values
   (1, 4);
Insert into Table_a
   (SEQ, INSTANCE)
 Values
   (2, 274);
Insert into Table_a
   (SEQ, INSTANCE)
 Values
   (3, 18);
Insert into Table_a
   (SEQ, INSTANCE)
 Values
   (4, 26);
Insert into Table_a
   (SEQ, INSTANCE)
 Values
   (5, 14);
COMMIT;
/

CREATE TABLE Table_b
(
  E_DATE              DATE,
  ACC                 VARCHAR2(8)      NOT NULL,
  INSTANCE              NUMBER            NOT NULL,
  TOTAL_VALUE             NUMBER(38)
)
/

SET DEFINE OFF;
Insert into Table_b
   (INSTANCE, E_DATE, ACC, TOTAL_VALUE)
 Values
   (14, TO_DATE('11/30/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'RR', 475.74);
Insert into Table_b
   (INSTANCE, E_DATE, ACC, TOTAL_VALUE)
 Values
   (18, TO_DATE('11/30/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'RR', 479.99);
Insert into Table_b
   (INSTANCE, E_DATE, ACC, TOTAL_VALUE)
 Values
   (26, TO_DATE('11/30/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'RR', 422.34);
COMMIT;
/

Results should display:
ACC				E_DATE			INSTANCE		TOTAL_VALUE
RR			30-Nov-2011			18				479.99

The result is so because the total_value figure differs according to the instance in Table_b
Re: Instance Sequence Priority Values [message #610102 is a reply to message #610100] Mon, 17 March 2014 11:21 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> with 
  2    data as (
  3      select a.seq, a.instance, b.e_date, b.acc, b.total_value
  4      from table_a a, table_b b
  5      where b.instance = a.instance
  6      order by a.seq
  7    )
  8  select acc, e_date, instance, total_value
  9  from data
 10  where rownum = 1
 11  /
ACC      E_DATE                INSTANCE TOTAL_VALUE
-------- ------------------- ---------- -----------
RR       30/11/2011 00:00:00         18         480

1 row selected.

Previous Topic: merge 2 rows
Next Topic: PL/SQL SYS_REFCURSOR Missing Data
Goto Forum:
  


Current Time: Fri Apr 26 13:46:34 CDT 2024