Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2011-02-15 : 16:50:14
|
| This must be simple, but it somehow escapes me...How to retrieve the earliest date in a set of rows when the sum of the set = amount?For example, the earliest date where the total $ of orders placed by CustID 5427 = $1500. SELECT MIN(DateEntered) FROM Orders WHERE custID = 5427 HAVING SUM(Charge) = 1500 returns the earliest date of the complete set if there is a total charge of 1500, rather than returning the earliest date the cumulative orders of this customer reached 1500. So for example if the customer placed an order for $500 in October, November, December, and January I don't want to see October, I want to see December because that's when the cumulative order total reached 1500. |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-02-16 : 00:38:36
|
| The below example should give you some fair idea:--Sample DataCreate Table SumTest( CustID Int, OrdDate DateTime, OrdVal int)Insert into SumTestSelect 1,'2010-10-05',500 unionSelect 1,'2010-11-04',500 unionSelect 1,'2010-12-05',500 unionSelect 1,'2011-01-02',500 unionSelect 2,'2010-10-05',1500 unionSelect 2,'2010-11-04',500 unionSelect 3,'2010-10-05',1000 unionSelect 3,'2010-10-20',200 unionSelect 3,'2010-11-20',500 --Sample Code;with CTEas(Select Row_number() over (Partition by CustID order by OrdDate) as Srno,CustID, OrdDate,OrdValfrom SumTest)Select Custid, Min(OrdDate) from(Select a.CustID,a.OrdDate,Sum(b.OrdVal) as RunTotfrom CTE a left join CTE bon a.CustID = b.CustID and b.srno < a.srno + 1group by a.CustID,a.OrdDate) as subtabwhere subtab.RunTot >=1500group by CustIdRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2011-02-16 : 12:21:32
|
| Thank you! A running total was exactly what was needed. |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2011-02-16 : 17:53:35
|
So it's not simple, and I stole it from someone on here and don't have a reference, but this is a FAST way to do running totals that could be adapted to suit your needs:IF OBJECT_ID('tempdb..#testCTE') IS NOT NULL DROP TABLE #testCTESELECT TOP 1000000 Number, CAST(0 AS BIGINT) RTINTO #testCTEFROM dbo.udfNumberAdvanced(1000000,1,1)ORDER BY NumberDECLARE @dtStart DATETIMESELECT @dtStart = GETDATE()-- create our temp table to return data from-- ORDER BY NEWID() -- also works just fine-- declare helper variableDECLARE @RT BIGINT;SELECT @RT = 0;WITH RunningTotals AS( SELECT TOP (2147483647) -- put a int max value here to get all rows Number, RT FROM #testCTE ORDER BY Number -- both of there work correctly -- ORDER BY Number DESC -- ORDER BY NEWID())UPDATE RunningTotals SET @RT = RT = @RT + NumberOUTPUT inserted.*SELECT DATEDIFF(s, @dtStart, GETDATE()) AS DurationInSeconds--IF OBJECT_ID('tempdb..#testCTE') IS NOT NULL-- DROP TABLE #testCTE |
 |
|
|
|
|
|
|
|