Unable to get the expected result from the sql server query.

  • Hello All,

    I am having three tables say table A, table B, table C of which table a and table b has data.

    table c does not have data but it relationship with table a and table b.

    I have written below query to get the expected data .

    SELECT h.HorseId,h.HorseName,

    sh.ShowHorsesId,sh.HorseCatalog,sh.IsActive,

    s.ShowName,s.ShowId

    FROM Horse h LEFT OUTER JOIN ShowHorses sh

    ON h.HorseId=sh.HorseId

    LEFT OUTER JOIN Show s

    ON sh.ShowId=s.ShowId

    but it's not giving the correct data.

    it is giving data as below

    HorseIdHorseNameShowHorsesIdHorseCatalogIsActiveShowNameShowId

    1Test NameNULLNULLNULLNULLNULL

    2Test NameNULLNULLNULLNULLNULL

    3Test NameNULLNULLNULLNULLNULL

    But i want it to be as below

    HorseIdHorseNameShowHorsesIdHorseCatalogIsActiveShowNameShowId

    1Test NameNULLNULLNULLArabian Show1

    2Test NameNULLNULLNULLArabian Show1

    3Test NameNULLNULLNULLArabian Show1

    Can anybody help me in achieving this.

  • As per your scenarios

    For example : you have data for tableA and tableC but you don't have data for TableB . And you don't have direct relationship for between TableA and Tablec.

    In this scenario.

    First it's join with tableA and tableB but tableB don't have any data . In this time we have to take left outer join then we will get data for all columns from tableA and tableB.it means all null values from TableB columns.

    second join with tableB and tableC,it will be happening same as above but it's don't have any matching rows for both joins.

  • Your question is not clear. And changing midway from TableA to Horses doesn't help either.

    Please visit this link: http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/[/url] to find out what information we need to help you, and how to collect and post that information.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

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

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