Monday, 15 March 2010

Sql Query Join in Oracle -



Sql Query Join in Oracle -

i have parent table , multiple kid tables foreign key constraint.

school table school id edudetails genders address_id edutype 1 2 m 3 fgn

and kid tables like

education details edu id educationtype 2 online aka name school id aka name 1 test school 1 school test gender table gender id gender desc m male

i using left outer bring together parent , school table fetch results.

but issue is, if aka table has 5 counts matching school id , gender table has 1 records school id.

so results comes 5 duplicate rows school info , other kid table information.

is there workaround prepare issue. tried using subquery , row_number on function. not working me. can help me solve issue.

thanks in advance time in looking issue.

my required output should this

school_id aka name genderdesc eductaiontype 1 test school male online 1 school test

so need have null values not matching records.

since want records in aka name table, i've joined on getting row_number each row. using row_number, left join on other tables.

select s.schoolid, sa.akaname, g.gendername, ed.educationtype school s bring together (select schoolid, akaname, row_number() on (partition schoolid order akaname) rn schoolaka ) sa on s.schoolid = sa.schoolid left bring together (select eduid, educationtype, row_number() on (order educationtype) rn edudetails ) ed on s.eduid = ed.eduid , sa.rn = ed.rn left bring together (select genderid, gendername, row_number() on (order gendername) rn genders ) g on s.genderid = g.genderid , sa.rn = g.rn

here sql fiddle.

and here results:

schoolid akaname gendername educationtype 1 school test male online 1 test school (null) (null)

sql oracle oracle11g

No comments:

Post a Comment