Help with Joins.....

  • I am trying to get a EmpID where the PayID is not null. My problem is that an employee may have several EmpID’s. So I am currently getting all ID’s for an employee. I only want the EmpID for ones that have a PayID. Any ideas?

    SELECT Distinct a.*,

    b.Station,

    c.EmpID

    FROM #NoStaffID as a

    Left JOIN #Station b

    On a.City = b.City

    JOIN Employee.Payroll c

    On a.City=c.City

    WHERE c.PayID is not null

  • In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • ccmret (7/9/2013)


    I am trying to get a EmpID where the PayID is not null. My problem is that an employee may have several EmpID’s. So I am currently getting all ID’s for an employee. I only want the EmpID for ones that have a PayID. Any ideas?

    SELECT Distinct a.*,

    b.Station,

    c.EmpID

    FROM #NoStaffID as a

    Left JOIN #Station b

    On a.City = b.City

    JOIN Employee.Payroll c

    On a.City=c.City

    WHERE c.PayID is not null

    Shouldn't at least one of your JOINs be ON empID?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • ccmret (7/9/2013)


    I am trying to get a EmpID where the PayID is not null. My problem is that an employee may have several EmpID’s.

    Gosh... unless you have a DateActivated and DateInactivated column, having multiple EmpIDs for the same employee is a fundamental problem. Are you sure there's no activation/deactivation dates associated with the EmpIDs?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Same as Jeff, I'm not sure what do you mean by employee having several EmpId's, however, your current query

    SELECT Distinct a.*,

    b.Station,

    c.EmpID

    FROM #NoStaffID as a

    Left JOIN #Station b

    On a.City = b.City

    JOIN Employee.Payroll c

    On a.City=c.City

    WHERE c.PayID is not null

    will only return records where PayID is present and not null.

    I have one question and one suggetsion to you:

    1. Why there is no join on EmpID to Payroll table? Surely you may have multiple Payroll records per city, looks like "cut & paste" issue to me.

    2. If you follow the order of JOINS from INNER to OUTER and use a bit better formatting, you will make your query much more readable and therefore more maintainable:

    SELECT DISTINCT a.*

    ,b.Station

    ,c.EmpID

    FROM #NoStaffID AS a

    INNER JOIN Employee.Payroll AS c

    ON c.City = a.City

    LEFT JOIN #Station AS b

    ON b.City = a.City

    WHERE c.PayID IS NOT NULL

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

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