Data Joins in SQL

  • Greetings,

    I'm dealing with a SQL Code that utilizes 3 tables, one with the original data which has 20,038 rows of data, and two tables which are mapping tables that I am running a left join on. The issue is when I run the query the results are coming back with more rows of data than the original table contains. What am I doing wrong that is causing duplicates to be created in my SQL results? Below are the summary of my results and the queries that I have been using:

    Summary of Results:

    Original Data: 20,038 rows of data

    1 Left Join: 23,182 rows of data

    2 Left Joins: 23,182 rows of data

    Original data Query:

    SELECT

    a.bocname

    ,(a.[Obs])as Obs

    ,(a.[FTE]) As FTE

    FROM [BudgetFormulation].[dbo].[ObjectClassFY14] a

    Where left(a.appro,4) = '0152'

    One Left Join Query:

    SELECT

    a.bocname

    ,(a.[Obs])as Obs

    ,(a.[FTE]) As FTE

    FROM [BudgetFormulation].[dbo].[ObjectClassFY14] a

    Left Join [BudgetFormulation].[dbo].[DIMObject] b

    On a.[BOC]=b.[BOC4]

    Where left(a.appro,4) = '0152'

    Two left Join Query:

    SELECT

    a.bocname

    ,(a.[Obs])as Obs

    ,(a.[FTE]) As FTE

    FROM [BudgetFormulation].[dbo].[ObjectClassFY14] a

    Left Join [BudgetFormulation].[dbo].[DIMObject] b

    On a.[BOC]=b.[BOC4]

    Left Join [BudgetFormulation].[dbo].[DIMSTA] c

    On a.[STA]=c.[Station]

    Where left(a.appro,4) = '0152'

  • check for duplicate values in the table you are joining to. The dups will be on the joining field.

  • That's exactly what was causing the issue. I had a duplicate in table b. Thanks for the assist!

    David

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

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