Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 sum every two number combination Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, September 24, 2012 11:29 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Friday, January 10, 2014 9:03 AM Points: 103, Visits: 289
 Let's say your coworker gave you the results of a query, and noted he/she did not include SalesOrderID numbers 43674 and 44295. However, you run that query: (from AdventureWorks)SELECT [SalesOrderID] , a.City ,[TotalDue]FROM [AdventureWorks].[Sales].[SalesOrderHeader] s INNER JOIN AdventureWorks.Person.Address a ON s.BillToAddressID = a.AddressIDWHERE City = 'Ottawa' AND SalesOrderID NOT IN ('43674','44295')And find that you're grand total for orders = \$98689.28, whereas the results the coworker sent has as grand total in sales of \$109276.67. So you know he/she made a mistake and excluded the wrong two SalesOrderIDs. Now, obviously, the solution is to call and ask them to open the query and verify the excuded IDs. That's exactly what I did, and issue is resolved. But I got it into my head that it would be cool to be able to somehow figure out the two excluded IDs by summing all two number combinations until you found the sum that matched the difference. This is more of a pet project or brain teaser I've given myself, but I'm not getting very far. A gentle nudge in the right direction would be nice.
Post #1363650
 Posted Monday, September 24, 2012 11:54 AM
 SSC-Addicted Group: General Forum Members Last Login: Wednesday, January 16, 2013 4:23 PM Points: 415, Visits: 2,333
 A wild guess just for fun! Cue all smarter folks to line up and show how inefficient this query is!SELECT TAB1.SalesOrderId AS ID1, TAB1.TotalDue AS DUE1, TAB2.SalesOrderId AS ID2, TAB2.TotalDue AS DUE2FROM SALESTABLE TAB1CROSS JOINSALESTABLE TAB2WHERE TAB1.SalesOrderID <> TAB2.SalesOrderID ANDTAB1.TotalDue + TAB2.Totaldue = @SPECIFIED_DIFFERENCE
Post #1363660
 Posted Monday, September 24, 2012 12:25 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Friday, January 10, 2014 9:03 AM Points: 103, Visits: 289
 You know, it worked. It tool 3.58 minutes, but it worked.
Post #1363675
 Posted Monday, September 24, 2012 12:31 PM
 SSC-Insane Group: General Forum Members Last Login: Today @ 3:15 PM Points: 22,475, Visits: 30,154
Post #1363677
 Posted Monday, September 24, 2012 12:37 PM
 SSCrazy Group: General Forum Members Last Login: Today @ 2:11 PM Points: 2,763, Visits: 5,900
 I don't have anything to test with, but I believe this could be better.However, the problem is that it's still using a cartesian product to obtain the result.SELECT TAB1.SalesOrderId AS ID1, TAB1.TotalDue AS DUE1, TAB2.SalesOrderId AS ID2, TAB2.TotalDue AS DUE2FROM SALESTABLE TAB1JOIN SALESTABLE TAB2 ON TAB1.SalesOrderID < TAB2.SalesOrderID --Avoid half of the rows.WHERE TAB1.TotalDue < @SPECIFIED_DIFFERENCE --This are useless unless you have negatives.AND TAB2.TotalDue < @SPECIFIED_DIFFERENCE --Same as above.AND TAB1.TotalDue + TAB2.Totaldue = @SPECIFIED_DIFFERENCEI'm not sure if the conditions added will help but I'm sure the condition for the join must help the performance. Luis C.I am a great believer in luck, and I find the harder I work the more I have of it. Stephen LeacockForum Etiquette: How to post data/code on a forum to get the best help
Post #1363678
 Posted Monday, September 24, 2012 12:42 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Friday, January 10, 2014 9:03 AM Points: 103, Visits: 289
 Lynn Pettis (9/24/2012)Can TotalDue every be less than 0?No. And there are no nulls.
Post #1363679
 Posted Monday, September 24, 2012 1:56 PM
 SSC-Insane Group: General Forum Members Last Login: Today @ 3:15 PM Points: 22,475, Visits: 30,154
 Luis Cazares (9/24/2012)I don't have anything to test with, but I believe this could be better.However, the problem is that it's still using a cartesian product to obtain the result.SELECT TAB1.SalesOrderId AS ID1, TAB1.TotalDue AS DUE1, TAB2.SalesOrderId AS ID2, TAB2.TotalDue AS DUE2FROM SALESTABLE TAB1JOIN SALESTABLE TAB2 ON TAB1.SalesOrderID < TAB2.SalesOrderID --Avoid half of the rows.WHERE TAB1.TotalDue < @SPECIFIED_DIFFERENCE --This are useless unless you have negatives.AND TAB2.TotalDue < @SPECIFIED_DIFFERENCE --Same as above.AND TAB1.TotalDue + TAB2.Totaldue = @SPECIFIED_DIFFERENCEI'm not sure if the conditions added will help but I'm sure the condition for the join must help the performance.What you have here is what I was thinking. If the total due for each order can not be less than zero, then it makes sense to restrict the testing to orders where the total due for each order is less than or equal to the total difference. If you could have a negative total, then this check would not be worthwhile.
Post #1363708
 Posted Monday, September 24, 2012 2:15 PM
 SSC-Addicted Group: General Forum Members Last Login: Wednesday, January 16, 2013 4:23 PM Points: 415, Visits: 2,333
 Or we could put an index on the totaldue column SELECT TAB1.SalesOrderId AS ID1, TAB1.TotalDue AS DUE1, TAB2.SalesOrderId AS ID2, TAB2.TotalDue AS DUE2FROM SALESTABLE TAB1JOINSALESTABLE TAB2ON TAB1.Totaldue = @SPECIFIED_DIFFERENCE - TAB2.Totaldue
Post #1363718
 Posted Monday, September 24, 2012 2:29 PM
 SSChampion Group: General Forum Members Last Login: Today @ 3:00 PM Points: 11,949, Visits: 10,981
 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. _______________________________________________________________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 Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1363723
 Posted Monday, September 24, 2012 6:57 PM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 8:24 PM Points: 3,589, Visits: 5,095
 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!DECLARE @Orders Table (OrderID VARCHAR(4), Amount MONEY)INSERT INTO @OrdersSELECT 2122, 5400UNION ALL SELECT '2123', 5500UNION ALL SELECT '2124', 1500UNION ALL SELECT '2125', 5700UNION ALL SELECT '2126', 4500UNION ALL SELECT '2126', 1500UNION ALL SELECT '2127', 5200UNION ALL SELECT '2129', 1000DECLARE @Amount MONEY = 7000;WITH UNIQUEnTuples (n, Tuples, Amount) AS ( SELECT 1 -- Add brackets around the OrderID to make later string comparison unique , CAST('[' + OrderID + ']' AS VARCHAR(max)) COLLATE Latin1_General_BIN ,Amount FROM @Orders UNION ALL SELECT 1 + n.n, a.OrderID + n.Tuples, n.Amount + t.Amount FROM @Orders t CROSS APPLY ( SELECT '[' + t.OrderID + ']') a(OrderID) JOIN UNIQUEnTuples n ON a.OrderID < n.Tuples WHERE CHARINDEX(a.OrderID, n.Tuples) = 0 AND n < 3 AND n.Amount + t.Amount <= @Amount)SELECT * FROM UNIQUEnTuplesWHERE Amount = @Amount -- The sum you're intereseted in identifyingFor an explanation of this approach, you can take a look at this article: http://www.sqlservercentral.com/articles/sql+n-Tuples/89809/I've found that the COLLATE I added improves performance slightly. You can modify n < 3 to account for the depth of nTuples you want to examine.Edit: Added the @Amount variable so I could use it to limit the rows examined in the recursive leg of the rCTE (potentially improves performance). My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!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?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1363754

 Permissions

 Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.