Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

sum every two number combination Expand / Collapse
Author
Message
Posted Monday, September 24, 2012 11:29 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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.AddressID
WHERE 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

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-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 DUE2
FROM SALESTABLE TAB1
CROSS JOIN
SALESTABLE TAB2
WHERE TAB1.SalesOrderID <> TAB2.SalesOrderID AND
TAB1.TotalDue + TAB2.Totaldue = @SPECIFIED_DIFFERENCE

Post #1363660
Posted Monday, September 24, 2012 12:25 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 23,215, Visits: 31,895
Can TotalDue every be less than 0?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1363677
Posted Monday, September 24, 2012 12:37 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 11:25 AM
Points: 3,477, Visits: 7,519
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 DUE2
FROM SALESTABLE TAB1
JOIN 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_DIFFERENCE

I'm not sure if the conditions added will help but I'm sure the condition for the join must help the performance.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum 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

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 23,215, Visits: 31,895
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 DUE2
FROM SALESTABLE TAB1
JOIN 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_DIFFERENCE

I'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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1363708
Posted Monday, September 24, 2012 2:15 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-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 DUE2
FROM SALESTABLE TAB1
JOIN
SALESTABLE TAB2
ON TAB1.Totaldue = @SPECIFIED_DIFFERENCE - TAB2.Totaldue

Post #1363718
Posted Monday, September 24, 2012 2:29 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:53 AM
Points: 13,238, Visits: 12,079
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

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:14 AM
Points: 3,618, Visits: 5,254
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 @Orders
SELECT 2122, 5400
UNION ALL SELECT '2123', 5500
UNION ALL SELECT '2124', 1500
UNION ALL SELECT '2125', 5700
UNION ALL SELECT '2126', 4500
UNION ALL SELECT '2126', 1500
UNION ALL SELECT '2127', 5200
UNION ALL SELECT '2129', 1000

DECLARE @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 UNIQUEnTuples
WHERE Amount = @Amount -- The sum you're intereseted in identifying


For 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
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse