Sum and Data matching

  • Sorry guys.. new to SQL so may be this question seems primitive to you.

    I have two tables that have one common field. I need to sum one field of one table (group by common field) and then compare it with column of another table. The final result should be the listing of the common field that does not get match (sum of one table group by column field vs column of another table)

    Need suggestion and advice.

  • If you expect that values will be missing from the non-summary table, do the summary first and a LEFT JOIN to the non-summary table. Columns that are NULL in the right table will be the ones missing from the summary table.

    If the relationship is the other way around, either query the non-summary table first or switch to a RIGHT JOIN.

    If values could be missing from either, this case would be a FULL JOIN.

    For more information, you'd need to follow best practices for posting questions and provide DDL, sample data and expected results and someone will undoubtedly come along and give you some tested code.


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

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