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 |
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2011-12-22 : 11:03:40
|
Hi,I have this table:ID type qty1 Purchase 102 Purchase 203 Sale 84 Purchase 155 Sale 26 Sale 8I want to create a view that would aggegate sum the qty on another column. Such as this:ID type qty Aggregate1 Purchase 10 102 Purchase 20 303 Sale 8 384 Purchase 15 535 Sale 2 556 Sale 8 63any help is appreciated. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-22 : 11:07:48
|
[code]SELECT t.ID, t.type, t.qty, t1.AggregateFROM Table tCROSS APPLY (SELECT SUM(Qty) AS [Aggregate] FROM Table WHERE ID < = t.ID )t1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-12-22 : 11:09:25
|
Doing running totals is one of those things that SQL is not very good at. Here is an alternate approach.;WITH cte AS( SELECT *,qty AS RunningTotal FROM YourTable UNION ALL SELECT t.*,c.RunningTotal+t.qty FROM YourTable t INNER JOIN cte c ON c.ID+1 = t.ID)SELECT * FROM cte OPTION (MAXRECURSION 0); |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-12-22 : 11:21:24
|
[code]CREATE TABLE #myTable99([ID] int, [type] varchar(20), [qty] int)GOINSERT INTO #myTable99([ID], [type], [qty])SELECT 1, 'Purchase', 10 UNION ALLSELECT 2, 'Purchase', 20 UNION ALLSELECT 3, 'Sale', 8 UNION ALLSELECT 4, 'Purchase', 15 UNION ALLSELECT 5, 'Sale', 2 UNION ALLSELECT 6, 'Sale', 8GOSELECT * FROM #myTable99GO SELECT t.[ID], t.[qty], t.[type], xxx.[SUM_qty] FROM ( SELECT l.[ID] , SUM(r.[qty]) AS [SUM_qty] FROM #myTable99 l INNER JOIN #myTable99 r ON r.[ID] <= l.[ID] GROUP BY l.[ID]) AS XXXINNER JOIN #myTable99 t ON xxx.[ID] = t.[ID] ORDER BY t.[id]GODROP TABLE #myTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
X002548
Not Just a Number
15586 Posts |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-12-22 : 11:34:40
|
Ugh! You are right, Brett. I promise, I will never post again without testing :-) .CREATE TABLE #myTable99(id INT, qty INT);INSERT INTO #myTable99 VALUES (1,10),(2,20),(3,8),(4,15),(5,2),(6,8);;WITH cte AS(SELECT *,qty AS RunningTotal FROM #myTable99 WHERE ID=1UNION ALLSELECT t.*,c.RunningTotal+t.qtyFROM #myTable99 t INNER JOIN cte c ON c.ID+1 = t.ID)SELECT * FROM cte OPTION (MAXRECURSION 0); GO |
|
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-12-22 : 11:38:06
|
yeah that works now...now if I just knew what the OPTION is...never seen that beforeis that a 2k8R2 compatibility?EDIT: Duh...never seen it used before with recursionquote: MAXRECURSION numberSpecifies the maximum number of recursions allowed for this query. number is a non-negative integer between 0 and 32767. When 0 is specified, no limit is applied. If this option is not specified, the default limit for the server is 100.
Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
|
|
|
|
|