Question

I am working on creating some queries for an existing database and I am having trouble trying to get certain attributes to show up in the query. I am sure my logic is wrong, but I've searched non-stop and can't seem to find a right way to search for the solution.

I am dealing with a COMMITTEE table, a COMMITTEEOWNER table, and then a UNIT, SCHOOL, and CAMPUS table. I am trying to display names from the unit, school, and campus tables in the query, along with five other attributes from the COMMITTEE table. The COMMITTEEOWNER table only has Owner_ID attributes that has relationships from the other four tables. Basically, the query should show Owner_ID, Name, Committee Name, Minimum Members, Max Members, and Type. The "Name" attribute should pull the data from NAME from Unit, School, and Campus tables. The "Committee Name" is from the Committee table. Here is what I have so far:

SELECT DISTINCT OWNER_ID, NAME "Committee Name", MIN_MEMBERS "Minimum Members", 
    MAX_MEMBERS "Max Members", TYPE "Type"
FROM COMMITTEE, COMMITTEEOWNER
WHERE OWNER_ID = COMMITTEE.COMMITTEEOWNER_OWNER_ID AND EXISTS
   (SELECT COMMITTEEOWNER_OWNER_ID, NAME
    FROM UNIT NATURAL JOIN COMMITTEEOWNER 
    UNION
    SELECT COMMITTEEOWNER_OWNER_ID, NAME
    FROM SCHOOL NATURAL JOIN COMMITTEEOWNER
    UNION
    SELECT COMMITTEEOWNER_OWNER_ID, NAME
    FROM CAMPUS NATURAL JOIN COMMITTEEOWNER)
ORDER BY OWNER_ID;

The query I am trying to conduct is: Create a list of all committees and their owner’s names. (All information from committee and owner name from the joined table(s).)

I cannot seem to figure out how to get the NAME from the tables that UNION'd in the parenthesis to show in my query. Any help would be much appreciated.

EDIT: More information about Table attributes: Table COMMITTEE has COMMITTEEOWNER_OWNER_ID, which is a FK from the OWNER_ID from COMMITTEEOWNER table, NAME, MIN_MEMBERS, MAX_MEMBERS, which are just integers that can be NULL, and then TYPE is a VARCHAR(15 BYTE) Data Type. The FK and Name make up the PK.

COMMITTEEOWNER only has one attribute, and that is the OWNER_ID which is the PK.

UNIT, CAMPUS, and SCHOOL tables have many attributes, but the only ones I am concerned with are the COMMITTEEOWNER_OWNER_ID FK that is in all three, which is from the COMMITTEEOWNER table, and then NAME. The NAME attribute in all three tables is the name of the particular unit, school, or campus. The primary key for School and Unit is made up of the Univeristy_code, campus_code, and code, and the campus has University_code and Campus_code as the PK.

Was it helpful?

Solution

Ok, you have to do all your joins in you FROM statement :

SELECT 
    CO.OWNER_ID
    , Educational_structures.NAME "Committee Name"
    , C.MIN_MEMBERS "Minimum Members"
    , C.MAX_MEMBERS "Max Members"
    , C.TYPE "Type"
FROM 
    COMMITTEEOWNER CO
    ,COMMITTEE C
    ,(  SELECT COMMITTEEOWNER_OWNER_ID, NAME
        FROM UNIT NATURAL JOIN COMMITTEEOWNER 
        UNION ALL
        SELECT COMMITTEEOWNER_OWNER_ID, NAME
        FROM SCHOOL NATURAL JOIN COMMITTEEOWNER
        UNION ALL
        SELECT COMMITTEEOWNER_OWNER_ID, NAME
        FROM CAMPUS NATURAL JOIN COMMITTEEOWNER) Educational_structures   
WHERE 
    OWNER_ID = COMMITTEE.COMMITTEEOWNER_OWNER_ID 
    AND OWNER_ID = Educational_structures.COMMITTEEOWNER_OWNER_ID

ORDER BY OWNER_ID;

Please let me know if you need something else ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top