Left join with duplicate records

  • Hi all,

    I am doing a left join betwen two tables, the issue is that the result of this join is bringing more rows that he left table has, from what i saw in the internet the issue resides on the right table which is making the left table adding some duplicates.

    Now how can i solve this??? 🙂

    Thanks,

    Daniel

  • daniel.carreira (8/22/2013)


    Hi all,

    I am doing a left join betwen two tables, the issue is that the result of this join is bringing more rows that he left table has, from what i saw in the internet the issue resides on the right table which is making the left table adding some duplicates.

    Now how can i solve this??? 🙂

    Thanks,

    Daniel

    This isn't an issue and it isn't something you can "solve". This is how joins work. If you have a row in the main table and two rows meet the join criteria in the second table it will return two rows.

    _______________________________________________________________

    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/

  • And this brings us to the BUSINESS REQUIREMENTS

    What is it that you are trying to do? Do you need just one value from the second table and in this case the second table is probably not sufficiently normalized, or do you want all values from the second table for a particular row in the first table?

    You can pre-process the second table to pre-aggregate your data and join to the result of pre-aggregation. For example if you are joining order and order details table, you can create a view, a function, a procedure(especially CLR procedure) that returns a nice formatted string with aggregated details for each order for your report, then you jion order information to this table with nice strings...

    Yelena

    Regards,Yelena Varsha

  • Depending on what your doing, the usual solution is to aggregate a field or two and then group the data. Can you supply a bit more information on what your joining and why and what your hoping to achieve from doing the join ?

  • What people are suggesting I generally refer to as "narrowing the JOIN criteria."

    There are a multitude of ways to do it. But the way you choose to do it depends on the relationships between the tables and often the underlying data itself.

    It could be as simple as adding additional criteria to the right of ON, grouping as suggested or (gasp!) using DISTINCT in some way shape or form.

    The way you choose to do it depends on your specific situation. If you post DDL and some sample data, it is likely someone will come along and suggest the "best" way from among the alternatives.


    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 5 posts - 1 through 4 (of 4 total)

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