Domanda

I have a problem with converting a standard SQL join into the old Oracle join syntax (please don't ask me why I need that). I would expect the same result, anyway, it is not:

Sample Data:

create table testing (
aid number(8),
bid number(8),
btext varchar(80));

insert into testing values (100,1,'text1a');
insert into testing values (100,2,'text1b');
insert into testing values (100,3,'text1c');
insert into testing values (200,19,'text2b');
insert into testing values (200,18,'text2a');
insert into testing values (300,4324,'text3a');
insert into testing values (500,80,'text4a');
insert into testing values (50,2000,'text5a');
commit;

Standard SQL:

select a.*,b.* from testing a
left outer join testing b
on (a.aid = b.aid and a.bid < b.bid)
order by a.aid, b.bid;

AID BID BTEXT   AID_1   BID_1   BTEXT_1
50  200 text5a  NULL    NULL    NULL
100 1   text1a  100     2       text1b
100 2   text1b  100     3       text1c
100 1   text1a  100     3       text1c
100 3   text1c  NULL    NULL    NULL
200 18  text2a  200     19      text2b
200 19  text2b  NULL    NULL    NULL
300 432 text3a  NULL    NULL    NULL
500 80  text4a  NULL    NULL    NULL

Oracle SQL:

select a.*,b.* from testing a, testing b
where a.aid = b.aid(+) 
and a.bid < b.bid
order by a.aid, b.bid;

AID BID BTEXT   AID_1   BID_1   BTEXT_1
100 1   text1a  100     2       text1b
100 2   text1b  100     3       text1c
100 1   text1a  100     3       text1c
200 18  text2a  200     19      text2b

How to get the same result of the standart SQL using Oracle's legacy syntax?

È stato utile?

Soluzione

Your Oracle-style statement needs the (+) operator also on the less-than condition, since that is also part of your join criteria in the standard-SQL version.

select a.*,b.* from testing a, testing b
where a.aid = b.aid(+) 
and a.bid < b.bid(+)
order by a.aid, b.bid;

See sqlfiddle here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top