Domanda

I am using Oracle SQL (TeraTerm), and I am trying to join specific information from two tables CONSULTANT, and PROJECT_CONSULTANT, and I need to retrieve only the employees who worked over 40 hours. Here are the tables

Project Consultant

    PROJECT_ID      CONSULTANT_ID   NUMBER_HOURS
    --------------- --------------- ------------
    94738949        49620928                   6
    45699847        34879223                  57
    45699847        95928792                  44
    45699847        04875034                  59
    19870398        49620928                  32
    30495394        95928792                  57
    30495394        07811473                  50
    62388923        07811473                  82

and Consultant

    CONSULTA NAME                             ZIP   START_DT
    -------- -------------------------------- ----- ---------
    CON_TITLE
    -------------------------
    49620928 Tom Jones                        39875 01-SEP-98
    Junior Consultant

    04875034 Jack Johnson                     29087 05-OCT-93
    Manager

    34879223 Lanny Harris                     03944 30-APR-04
    Principal


    CONSULTA NAME                             ZIP   START_DT
    -------- -------------------------------- ----- ---------
    CON_TITLE
    -------------------------
    95928792 Michael Johnson                  02953 22-JUN-02
    Senior Manager

    07811473 Wendy Adams                      29087 05-JUL-05
    Senior Consultant

The code I came up with is

    select Consultant_ID, Name, Zip, and Number_Hours 
    from Consultant
    Inner Join project_consultant
    ON Consultant.Consultant_ID=project_consultant.Consultant_ID
    WHERE project_consultant.number_Hours>40;

I am getting an error

    ERROR at line 1:
    ORA-00936: missing expression

I just wanna know how to write the join statement correctly any help would be awesome, because I am having trouble knowing how to fix this join statement

È stato utile?

Soluzione

You don't use and in the select clause:

select c.Consultant_ID, c.Name, c.Zip, pc.Number_Hours 
from Consultant c Inner Join
     project_consultant pc
     on c.Consultant_ID = pc.Consultant_ID
where pc.number_Hours > 40;

You also need a table alias in the select clause to be clear what table Consultant_Id refers to.

EDIT:

You might actually want to sum the hours for employees. If so, you need an aggregation:

select c.Consultant_ID, c.Name, c.Zip, sum(pc.Number_Hours)
from Consultant c Inner Join
     project_consultant pc
     on c.Consultant_ID = pc.Consultant_ID
group by c.Consultant_ID, c.Name, c.Zip
having sum(pc.number_Hours) > 40;

Altri suggerimenti

You can't use and in Select Clause

Try this

    SELECT C.Consultant_ID, C.Name, C.ip, PC.Number_Hours 
    FROM Consultant C
    INNER Join project_consultant PC
    ON C.Consultant_ID=PC.Consultant_ID
    WHERE PC.number_Hours > 40;
    select c.Consultant_ID, c.Name, c.Zip, p.Number_Hours 
    from Consultant c
    Inner Join project_consultant p
    ON c.Consultant_ID=p.Consultant_ID
    WHERE p.number_Hours>40;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top