How to Join the two table

  • Table 1

    ABCDF

    A1B1NULLNULLNULL

    A2B2NULLNULLNULL

    A3B3NULLNULLNULL

    A4B4NULLNULLNULL

    A5B5NULLNULLNULL

    Table 2

    LocName

    A1White

    A2Black

    A3Red

    A4Orange

    A5Blue

    B1Green

    B2Yellow

    B3Gold

    B4Rose

    B5Silver

  • Use an OR condition in your join statement?

    _______________________________________________________________

    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/

  • ramanathan.raman88 (2/2/2013)


    Table 1

    ABCDF

    A1B1NULLNULLNULL

    A2B2NULLNULLNULL

    A3B3NULLNULLNULL

    A4B4NULLNULLNULL

    A5B5NULLNULLNULL

    Table 2

    LocName

    A1White

    A2Black

    A3Red

    A4Orange

    A5Blue

    B1Green

    B2Yellow

    B3Gold

    B4Rose

    B5Silver

    So your just asking how to join the two tables..?

  • Hi,

    What are trying to do? Use Left / Right / Inner joins as required.

    SELECT A,B,C,D,E,F,Name FROM TABLE1 LEFT JOIN TABLE2 ON A=Loc

    SELECT A,B,C,D,E,F,Name FROM TABLE1 RIGHT JOIN TABLE2 ON Loc=A

    SELECT A,B,C,D,E,F,Name FROM TABLE1 INNER JOIN TABLE2 ON A=Loc

    Refer: http://www.w3schools.com/sql/sql_join.asp

  • ramanathan.raman88 (2/2/2013)


    Table 1

    ABCDF

    A1B1NULLNULLNULL

    A2B2NULLNULLNULL

    A3B3NULLNULLNULL

    A4B4NULLNULLNULL

    A5B5NULLNULLNULL

    Table 2

    LocName

    A1White

    A2Black

    A3Red

    A4Orange

    A5Blue

    B1Green

    B2Yellow

    B3Gold

    B4Rose

    B5Silver

    The absolute best way would be to normalize Table 1. 😉

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

  • What are you expecting?

  • Looks like homework, but if what you're after is the Names associated with columns A and B, perhaps a correlated sub-query would be best:

    CREATE TABLE #Table1

    (A CHAR(2), B CHAR(2), C VARCHAR(20), D VARCHAR(20), F VARCHAR(20));

    INSERT INTO #Table1

    SELECT 'A1','B1',NULL, NULL, NULL

    UNION ALL SELECT 'A2','B2',NULL, NULL, NULL

    UNION ALL SELECT 'A3','B3',NULL, NULL, NULL

    UNION ALL SELECT 'A4','B4',NULL, NULL, NULL

    UNION ALL SELECT 'A5','B5',NULL, NULL, NULL;

    CREATE TABLE #Table2

    (Loc CHAR(2), Name VARCHAR(20));

    INSERT INTO #Table2

    SELECT 'A1','White'

    UNION ALL SELECT 'A2','Black'

    UNION ALL SELECT 'A3','Red'

    UNION ALL SELECT 'A4','Orange'

    UNION ALL SELECT 'A5','Blue'

    UNION ALL SELECT 'B1','Green'

    UNION ALL SELECT 'B2','Yellow'

    UNION ALL SELECT 'B3','Gold'

    UNION ALL SELECT 'B4','Rose'

    UNION ALL SELECT 'B5','Silver';

    SELECT A, B

    ,NameOfA=(SELECT Name FROM #Table2 b WHERE a.A = b.Loc)

    ,NameOfB=(SELECT Name FROM #Table2 b WHERE a.B = b.Loc)

    FROM #Table1 a;

    GO

    DROP TABLE #Table1, #Table2;


    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

Viewing 7 posts - 1 through 6 (of 6 total)

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