Sql Query for retriving proper data based on join condition

  • Hi All,

    I have two tables Emp and Loc and pick the data from these two tables based on joining condition of empid on both the tables.After that i have to check the values with one lookup table Job and pick the proper Jname and jdesc for the respective codes.Here empcode,deptcode,loccode needs to be join with JID for getting the required details,If corresponding empcode,deptcode,loccode not found in job table it will populate as UNK and unknown in codes  and desc fields..In this case how to apply multiple joins on different tables.Any one can guide me.Please check below for more details.
    employee:

    Empid

    FNAMELNAMEEMPCODEDEPTCODE
    1AG10100
    2BH20200
    3CI30300
    4DJ40400
    5EK50500
    6FL60600
    7XY10002000

    location:

    EMPID

    LOCNBRLOCCODE
    12232
    22333
    32434
    42535
    52636
    62737
    82838

    Job:

    JID

    JNAMEJDESC
    10ABTest1
    20BCTest2
    30CDTest3
    40DETest4
    50EFTest5
    100VWTest15
    200XYTest16
    300ABCTest17
    400DEFTest18
    500GHITest19
    32JKTest8
    33LMTest9
    34MNTest10
    35OPTest11
    36QRTest12
    1000HITest7
    2000MNOTest21
    38TUTest14
    7000PQRTest22
    8000STUTest23

    Output:

    Empid

    FNAMELNAMELOCNBREMPCODEEMPJNAMEEMPJDESCDEPTCODEDEPTJNAMEDEPTJDESCLOCCODELOCJNAMELOCJDESC
    1AG2210ABTest1100VWTest1532JKTest8
    2BH2320BCTest2200XYTest1633LMTest9
    3CI2430CDTest3300ABCTest1734MNTest10
    4DJ2540DETest4400DEFTest1835OPTest11
    5EK2650EFTest5500GHITest1936QRTest12
    6FL2760UNKUNKNOWN600UNKUNKNOWN37UNK UNKNOWN

    Tried with below querybut not able to get proper solution:
    select t.empid,t.fname,t.lname,t.empcode,j1.jname as EMPJNAME,j1.jdesc as EMPJDESC from job j1 right join

    (SELECT a.empid,a.fname,a.lname,a.empcode,a.deptcode,b.locnbr,b.lcocode from emp a
    inner join loc b
    on a.empid=b.empid)as t on j1.empcode=t.jid

    Scripts for refrence:

    Create table emp(
    empid varchar(50),
    FNAME varchar(50),
    LNAME varchar(50),
    EMPCODE varchar(50),
    DEPTCODE varcharr(50));

    Create table loc(
    empid varchar(50),
    LOCNBR varchar(50),
    LOCCODE varchar(50));

    Create table job(
    Jid varchar(50),
    JNAME varchar(50),
    JDESC varchar(50));

    INSERT into emp VALUES (1, 'A', 'G', 10, 100);

    INSERT into emp VALUES (2, 'B', 'H', 20, 200);

    INSERT into emp VALUES (3, 'C', 'I', 30, 300);

    INSERT into emp VALUES (4, 'D', 'J', 40, 400);

    INSERT into emp VALUES (5, 'E', 'K', 50, 500);

    INSERT into emp VALUES (6, 'F', 'L', 60, 600);

    INSERT into emp VALUES (7, 'X', 'Y', 1000, 2000);

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

    INSERT into loc VALUES (1, 22, 32);

    INSERT into loc VALUES (2, 23, 33);

    INSERT into loc VALUES (3, 24, 34);

    INSERT into loc VALUES (4, 25, 35);

    INSERT into loc VALUES (5, 26, 36);

    INSERT into loc VALUES (6, 27, 37);

    INSERT into loc VALUES (8, 28, 38);

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

    INSERT into job values (10, 'AB', 'Test1');

    INSERT into job values (20, 'BC', 'Test2');

    INSERT into job values (30, 'CD', 'Test3');

    INSERT into job values (40, 'DE', 'Test4');

    INSERT into job values (50, 'EF', 'Test5');

    INSERT into job values (100, 'VW', 'Test15');

    INSERT into job values (200, 'XY', 'Test16');

    INSERT into job values (300, 'ABC', 'Test17');

    INSERT into job values (400, 'DEF', 'Test18');

    INSERT into job values (500, 'GHI', 'Test19');

    INSERT into job values (32, 'JK', 'Test8');

    INSERT into job values (33, 'LM', 'Test9');

    INSERT into job values (34, 'MN', 'Test10');

    INSERT into job values (35, 'OP', 'Test11');

    INSERT into job values (36, 'QR', 'Test12');

    INSERT into job values (1000, 'HI', 'Test7');

    INSERT into job values (2000, 'MNO', 'Test21');

    INSERT into job values (38, 'TU', 'Test14');

    INSERT into job values (7000, 'PQR', 'Test22');

    INSERT into job values (8000, 'STU', 'Test23');

  • You can join on many tables, that's not a problem. So, you could easily have a query that looks like:
    SELECT E.Name,
           D.Department,
           ISNULL(J.Name, 'No Assigned Role') AS JobRole
    FROM Employee E
        JOIN Department D ON E.DepartmentID = D.DeparmentID
        LEFT JOIN Job J ON E.EmployeeID = J.EmployeeID;

    This would JOIN the Employees table to both the Department and Job tables, however, if no job for an employee is found, it will still return the Employee (as it is a LEFT JOIN).

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Great job on posting the sample data, but your query has several invalid object name errors in it.  If you could post a query that runs (even if it returns the wrong results), please, we should be able to show you where you're going wrong.

    John

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply