Jump to content

please help with joins


tomasd

Recommended Posts

Hi, I'm having 3 tables;

 

SQL> describe event_staff;

Name                                      Null?    Type

----------------------------------------- -------- ----------------------------

STAFF_ID                                  NOT NULL NUMBER(38)

EVENT_ID                                  NOT NULL VARCHAR2(2)

 

SQL> describe event_staff_agy;

Name                                      Null?    Type

----------------------------------------- -------- ----------------------------

STAFF_AGY_ID                              NOT NULL NUMBER(38)

EVENT_ID                                  NOT NULL VARCHAR2(2)

 

SQL> describe event;

Name                                      Null?    Type

----------------------------------------- -------- ----------------------------

LOCATION                                  NOT NULL VARCHAR2(10)

ID                                        NOT NULL VARCHAR2(2)

CLIENT_ID                                NOT NULL NUMBER(38)

 

SQL> select * from event_staff;

 

  STAFF_ID EV

---------- --

        1 1

        2 1

 

SQL> select * from event_staff_agy;

 

STAFF_AGY_ID EV

------------ --

          1 1

          2 1

 

SQL> select * from event;

 

LOCATION  ID  CLIENT_ID

---------- -- ----------

Loc 1      1          1

Loc 2      2          1

 

 

I'm trying to join those tables using;

SQL> SELECT

        EVENT.ID,

        EVENT_STAFF.STAFF_ID,

        EVENT_STAFF_AGY.STAFF_AGY_ID

FROM(

(EVENT INNER JOIN EVENT_STAFF ON EVENT.ID = EVENT_STAFF.EVENT_ID)

INNER JOIN EVENT_STAFF_AGY ON EVENT.ID = EVENT_STAFF_AGY.EVENT_ID

);

  2    3    4    5    6    7    8

ID  STAFF_ID STAFF_AGY_ID

-- ---------- ------------

1          1            1

1          1            2

1          2            1

1          2            2

 

I guess this is not correct as the result I'm expecting is;

ID  STAFF_ID STAFF_AGY_ID

-- ---------- ------------

1          1           

1          2           

1                        1

1                        2

 

what would be the right way of joining this?

Any help is appreciated!

Link to comment
Share on other sites

To get the results you expect you would need a UNION

SELECT
        EVENT.ID,
        EVENT_STAFF.STAFF_ID,
        '' AS STAFF_AGY_ID
FROM
EVENT INNER JOIN EVENT_STAFF ON EVENT.ID = EVENT_STAFF.EVENT_ID
UNION
SELECT
        EVENT.ID,
        '' AS STAFF_ID,
        EVENT_STAFF_AGY.STAFF_AGY_ID
FROM
EVENT
INNER JOIN EVENT_STAFF_AGY ON EVENT.ID = EVENT_STAFF_AGY.EVENT_ID

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.