Outer Join and MAX [message #612094] |
Fri, 11 April 2014 18:51 |
|
OraFaq13
Messages: 14 Registered: May 2013
|
Junior Member |
|
|
I wanted to pick the matching records for a detail table and if the detail table has multiple records for a program ID, then the most recent status from the detail table should be picked.
Master Table:
CREATE TABLE program_sa
(PGM_ID NUMBER, PGM_NAME VARCHAR2(10)
);
ALTER TABLE program_sa ADD CONSTRAINT pk1 PRIMARY KEY(pgm_id);
Detail Table:
CREATE TABLE program_assn
(
PGM_ID NUMBER,
BEGIN_DT DATE,
END_DATE DATE,
STATUS VARCHAR2(3),
CONSTRAINT fk1 FOREIGN KEY(pgm_id) REFERENCES program_sa(pgm_id)
);
Insert Statement:
-- inserts for master
INSERT INTO program_sa VALUES
(1,'Health'
);
INSERT INTO program_sa VALUES
(2,'Education'
);
INSERT INTO program_sa VALUES
(3,'Sports'
);
-- for detail table
INSERT INTO program_assn VALUES
(1,'01-Jan-2014', '10-Jan-14', 'AC'
);
INSERT INTO program_assn VALUES
(1,'11-Jan-2014', '20-Jan-14', 'PE'
);
INSERT INTO program_assn VALUES
(1,'21-Jan-2014', '31-Jan-14', 'DS'
);
When I outer join the detail table by ID , it picks all the programs from program_sa table and its corresponding records from program_assn.
SELECT *
FROM program_sa p ,
program_assn pa
WHERE p.PGM_ID = pa.PGM_ID(+)
;
When I use the outer join to the begin_dt to pick the most recent from the detail table, it fails with an error message
SELECT *
FROM program_sa p ,
program_assn pa
WHERE p.PGM_ID = pa.PGM_ID(+)
AND pa.begin_dt(+) IN
(SELECT MAX(begin_dt) FROM PROGRAM_ASSN pa1 GROUP BY pa1.pgm_id
)
;
Error:
ORA-01799: a column may not be outer-joined to a subquery
01799. 00000 - "a column may not be outer-joined to a subquery"
*Cause: <expression>(+) <relop> (<subquery>) is not allowed.
*Action: Either remove the (+) or make a view out of the subquery.
In V6 and before, the (+) was just ignored in this case.
Error at Line: 32 Column: 2
CAn anyone help me to pick the most recent status from the detail table?
|
|
|
|
|
|
|
|
Re: Outer Join and MAX [message #612106 is a reply to message #612104] |
Sat, 12 April 2014 02:37 |
|
OraFaq13
Messages: 14 Registered: May 2013
|
Junior Member |
|
|
I have changed my insert statement not to use the strings for date column. Again, I am not sure why it worked for me earlier and not you. May be my DB version(11g) is different from yours. However , thanks for the link.
INSERT
INTO program_assn VALUES
(
1,
TRUNC(SYSDATE,'RRRR'),
TRUNC(SYSDATE,'RRRR') + 9 ,
'AC'
);
INSERT
INTO program_assn VALUES
(
2,
TRUNC(SYSDATE,'RRRR') +10,
TRUNC(SYSDATE,'RRRR') +19 ,
'PE'
);
INSERT
INTO program_assn VALUES
(
3,
TRUNC(SYSDATE,'RRRR')+20,
LAST_DAY(TRUNC(SYSDATE,'RRRR')) ,
'DS'
);
|
|
|
|
|
|
Re: Outer Join and MAX [message #612118 is a reply to message #612117] |
Sat, 12 April 2014 05:11 |
|
OraFaq13
Messages: 14 Registered: May 2013
|
Junior Member |
|
|
I did updated my feedback/Thanks to the earlier posts. I did made mistake by not responding to those replies quickly but will correct that going forward. Thanks to you too for all the help.
|
|
|