combining tables

  • I had a question someone asked me the other day that stumped me a bit. I know it's something fairly easy, but I couldn't get it.

    If I have a table of employee salaries from 2010, and another table for 2011, how can I combine the tables in a query that will give me the common results, a col for 2010 salary and one for 2011 salary?

    2010 table:

    employeeID, dateHired, employeeName, salary

    2011 table:

    employeeID, dateHired, employeeName, salary

    resultTable:

    employeeID, dateHired, employeeName, 2010salary, 2011salary

    I made an attempt, but the only thing I could come up with was to right join to get all of the employees in the 2011 table, but that leaves out the people who were let go before 2011 who have 2010 salaries. If I do it the other way, I leave out anyone hired in 2011.

    Again, I know this is something fairly simple.

    Thanks!

  • You should use FULL OUTER JOIN!

    _____________________________________________
    "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]

  • Damn it, I knew it was something like that. Thanks. I have to be honest, I've never used it before.

  • Actually, you may not need to use it if you have dedicated Employee table (which most likely you do have). Then your query can use just left joins, something like:

    SELECT e.employeeID AS employeeID

    ,ISNULL(s10.dateHired, s11.dateHired) AS dateHired

    ,s10.salary AS 2010salary

    ,s11.salary AS 2011salary

    FROM Employee AS e

    LEFT JOIN Salary2010 AS s10

    ON s10.employeeID = e.employeeID

    LEFT JOIN Salary2011 AS s11

    ON s11.employeeID = e.employeeID

    Also, the dateHired will be better placed in the Employee table (is any reason for its denormalization?).

    _____________________________________________
    "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]

  • It would have been much easier if I did, but in the example I was given, it was just two tables of data I had to combine. Thanks tho, that makes a lot of sense.

  • try this:

    [font="System"]SELECT

    employeeID, dateHired, employeeName, max([2010Salary]), max([2011Salary])

    FROM (

    SELECT

    employeeID, dateHired, employeeName, salary as [2010Salary], 0 as [2011Salary]

    FROM [2010Table]

    UNION

    SELECT

    employeeID, dateHired, employeeName, 0 as [2010Salary], salary as [2011Salary]

    FROM [2011Table]

    )x

    GROUP BY

    employeeID, dateHired, employeeName[/font]

  • slowder (3/20/2012)


    try this:

    [font="System"]SELECT

    employeeID, dateHired, employeeName, max([2010Salary]), max([2011Salary])

    FROM (

    SELECT

    employeeID, dateHired, employeeName, salary as [2010Salary], 0 as [2011Salary]

    FROM [2010Table]

    UNION

    SELECT

    employeeID, dateHired, employeeName, 0 as [2010Salary], salary as [2011Salary]

    FROM [2011Table]

    )x

    GROUP BY

    employeeID, dateHired, employeeName[/font]

    ????? Why ?????

    1. It will not work, if dateHired is only populated in the year employee was employed

    2. It will be slower than FULL OUTER JOIN

    _____________________________________________
    "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]

  • Just a tip. The two jears worth of employee data probably shouldn't be in separate tables to begin with. Understanding that it may not be possible to actually rectify that problem, you might want to look into "Partitioned Views" for future queries so that all you have to do is change the content of a view to make yearly changes instead of changing code.

    --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)

  • Oh, don't get me wrong. I would never set it up like that. It was a question that someone came to me with to see if I could answer it. To tell the truth, I think it might have been an interview question 🙂

  • Matthew Cushing (3/20/2012)


    Oh, don't get me wrong. I would never set it up like that. It was a question that someone came to me with to see if I could answer it. To tell the truth, I think it might have been an interview question 🙂

    interview? i would have gone with a different schema

    CREATE TABLE NewPay (

    employeeID INT, -- Or what ever data type it really is

    dateHired DATETIME,

    employeeName VARCHAR(64),

    PayYear INT,

    Salary Numeric(8,2), -- or how ever many places you need

    CONSTRAINT CK_Year CHECK (PayYear LIKE [1-2][0-9][0-9][0-9]) -- validate a 4 digit year

    )

    INSERT INTO NewPay

    SELECT employeeID, dateHired, employeeName, 2010, Salary

    FROM 2010salary

    UNION ALL

    SELECT employeeID, dateHired, employeeName, 2011, Salary

    FROM 2011salary

    gets us normalized to boot (not having a column for each year of salary)


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • show off 🙂

  • Matthew Cushing (3/20/2012)


    show off 🙂

    why not show off a little. maby some newbie will search the forum (LOL) and find this reply and go wow thats cool, did not know i could do that.

    (Its how i came up with it, well googled and found it but still.)

    😛


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • another good point 🙂

    Thanks for finding that for me.

Viewing 13 posts - 1 through 12 (of 12 total)

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