sum every two number combination

  • Amy.G (9/25/2012)


    Thank you for this solution. I had originally tried to do a recursive cte, but though I'm beginning to understand how they work, I haven't been able to succussfully create my own from scratch. The logic of pulling data from something that doesn't really exist yet is a little much for my linear brain to fully embrace. I'm trying though!

    Here's another article for the recursively challenged (such as you and me). 🙂

    http://www.sqlservercentral.com/articles/T-SQL/90955/

    Instead of trying to explain how they work, it explores a set of simple to complex examples that may assist you in developing a working repetoire to draw from.


    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

  • Sean Lange (9/25/2012)


    ChrisM@Work (9/25/2012)


    dwain.c (9/24/2012)


    Sean Lange (9/24/2012)


    And of course if the exact rows matter this can get a LOT more challenging because there may be more than 1 pair where the sum is the total you are looking for.

    I can do that! And I can examine triplet combinations (or deeper) as well!

    ...

    When you wrote that excellent article Dwain, did you have any idea how often you would have to quote it? I think you would have said "about once a year". In practice it seems like once a week 😀

    I knew that article from Dwain would have an answer. Of course my point got lost in the shuffle. The OP said something about knowing WHICH IDs made the sum. My point was that because of the possibility of multiple pairs getting the EXACT IDs would be impossible. Of course you would have to use Dwain's technique to find them. 😉

    I am always pretty humbled :blush: when experienced and distringuished posters such as yourself indicate that they know of my work. Thanks!


    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 - 16 through 16 (of 16 total)

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