• Messi (8/23/2016)


    ChrisM@Work (8/23/2016)


    You have two rows in each table where Risk_01_Id_Num = '201600015'. Your result set with four rows is expected behaviour. If you wish to limit the result set to two rows, then you first have to decide from which table you will take two rows, and from which you will take only one: although Risk_01_Id_Num is the same for two rows in each table, there are column differences. Use those differences to decide which row to take. There are numerous ways of achieving this in TSQL, but first you must decide.

    why it is showing 4 rows ? should'nt it show 4 columns with 2 rows instead place legal comp columns after objected impacted columns?

    This should show you why 4 rows are returned:

    CREATE TABLE #Table1 (ID_t1 INT IDENTITY(1,1), Risk_01_Id_Num VARCHAR(10))

    INSERT INTO #Table1 (Risk_01_Id_Num) VALUES ('201600015'), ('201600015')

    CREATE TABLE #Table2 (ID_t2 INT IDENTITY(1,1), Risk_01_Id_Num VARCHAR(10))

    INSERT INTO #Table2 (Risk_01_Id_Num) VALUES ('201600015'), ('201600015')

    SELECT *

    FROM #Table1 t1

    INNER JOIN #Table2 t2

    ON t2.Risk_01_Id_Num = t1.Risk_01_Id_Num

    If not, can you adjust the model to reflect your environment? Thanks.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden