Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> full outer join with multiple key fields
How do you do a full outer join when the unique key is made up of more
than one field?
I have a table that holds a list of users and what rights they have to run certain programs. The database shows only those that do not have rights (N) or read only rights (R). If the user has complete rights to the program, there is no entry (no record) for that user/program combination.
My table I set up for testing is called PGM_RIGHTS and has the following 5 records of data.
Row User Pgm Allow
1 1 A N 2 1 B N 3 2 B N 4 3 B R 5 3 C N
By the time I get this figured out, I need to have it show up as grid deployed in Crystal Reports:
A B C
1 | N N Y 2 | Y N Y 3 | Y R N
As you can see, there are only 5 records of data, but I need 9 (three by three grid).
If the ALLOW column has data, I need to show it; otherwise (if there is no entry in the database) then I need it to show a "Y".
What I am now trying to do is create some kind of a JOIN that would give me something like the following recordset:
Row User Pgm Allow
1 1 A N 2 1 B N 3 1 C <NULL> 4 2 A <NULL> 5 2 B N 6 2 C <NULL> 7 3 A <NULL> 8 3 B R 9 3 C N
I tried to do this as an outer join. But my understanding of outer joins allows only one KEY field to join the two tables, and I have a combination of USER and PGM that is unique. I haven't been able to figure that one out.
I can get a Cartesian product (this is the first time I have ever wanted one!) by using nested select statements like this:
Select * from
(Select DISTINCT(User) from PGM_RIGHTS),
(Select DISTINCT(PGM) from PGM_RIGHTS)
That shows up like this.
Row User Pgm 1 1 A 2 1 B 3 1 C 4 2 A 5 2 B 6 2 C 7 3 A 8 3 B 9 3 C
Which is perfect, but there is no ALLOW column. When I try to add another field, it changes the database.
Is there a really smart SQL programmer out there who could give me some new insight into this little issue?
Weird Note: My first attempt was to pull the table into Crystal Reports (exactly as it stands with 5 data records) and turn it into a Cross Tab. It worked somewhat (like the following grid) showing blanks where there was no data found (that's where I need the Y.) I thought I could create Crystal formulas to check for NULL and return a Y, but the formula never gets checked because the blank spaces are not data (they are not a record with data, nor a record with NULL data, rather, they are no record at all (since there are only 5 records in the database) which causes Crystal to totally ignore any formulas or formatting for those blanks.
A B C
1 | N N 2 | N 3 | R NReceived on Mon Jul 26 2004 - 13:46:46 CDT