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 |
|
drpkrupa
Yak Posting Veteran
74 Posts |
Posted - 2006-10-09 : 17:54:25
|
| I have table with follwing colu and rows of data.Year account# Div Balance Month 2006 6010 DI -3265.48000 1 2006 6010 DI -1778.80000 22006 6010 DI -3124.83000 3 2006 6010 DI -22.67000 42006 6011 DI -35.48000 1 2006 6011 DI -178.80000 22006 6011 DI -31.83000 3 2006 6011 DI -25.67000 42006 6012 DI 3265.48000 1 2006 6012 DI 1778.80000 22006 6012 DI 3124.83000 3 2006 6012 DI 2572.67000 4I need answer of my query like. I want a add one more colu at the end which containt total balance2006 6010 DI -3265.48000 1 -3265.480002006 6010 DI -1778.80000 2 -5044.282006 6010 DI -3124.83000 3 -8169.112006 6010 DI -2572.67000 4 -10741.78That last column i want a add balace for piror month too. LikeJan - JanFen - Jan+FebMar - Jan-+Feb+Maretc.. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-10 : 04:02:53
|
| [code]-- Prepare test dataDECLARE @Test TABLE ([Year] SMALLINT, [Account#] SMALLINT, Div VARCHAR(2), Balance SMALLMONEY, [Month] TINYINT)INSERT @TestSELECT 2006, 6010, 'DI', -3265.48000, 1 UNION ALLSELECT 2006, 6010, 'DI', -1778.80000, 2 UNION ALLSELECT 2006, 6010, 'DI', -3124.83000, 3 UNION ALLSELECT 2006, 6010, 'DI', -22.67000, 4 UNION ALLSELECT 2006, 6011, 'DI', -35.48000, 1 UNION ALLSELECT 2006, 6011, 'DI', -178.80000, 2 UNION ALLSELECT 2006, 6011, 'DI', -31.83000, 3 UNION ALL SELECT 2006, 6011, 'DI', -25.67000, 4 UNION ALLSELECT 2006, 6012, 'DI', 3265.48000, 1 UNION ALLSELECT 2006, 6012, 'DI', 1778.80000, 2 UNION ALLSELECT 2006, 6012, 'DI', 3124.83000, 3 UNION ALLSELECT 2006, 6012, 'DI', 2572.67000, 4-- Do the workSELECT t1.[Year], t1.[Month], t1.[Account#], t1.Balance, (SELECT SUM(t2.Balance) FROM @Test t2 WHERE t2.[Year] = t1.[Year] AND t2.[Account#] = t1.[Account#] AND t2.[Month] <= t1.[Month]) RunningTotalFROM @Test t1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
drpkrupa
Yak Posting Veteran
74 Posts |
Posted - 2006-10-10 : 10:13:35
|
| Thanks for you help. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-10 : 10:21:10
|
It seems Cursor is more faster in this case if you read the link I posted MadhivananFailing to plan is Planning to fail |
 |
|
|
drpkrupa
Yak Posting Veteran
74 Posts |
Posted - 2006-10-10 : 11:43:05
|
| I tried to access that link and gave me error.I have one more question on the same query.SELECT t1.[Year],t1.[Month],t1.[Account#],t1.Balance, (SELECT SUM(t2.Balance) FROM @Test t2 WHERE t2.[Year] = t1.[Year] AND t2.[Account#] = t1.[Account#] AND t2.[Month] <= t1.[Month]) RunningTotalFROM @Test t1This query works fine but the problem is if i have account with missing month then i want a add row and add display the previous month balance.Ex:2006 6010 -10 1 2006 6010 -20 22006 6010 -30 4 2006 6010 -40 62006 6011 10 1 2006 6011 20 3In this case query retun:6010 1 -106010 2 -306010 4 -606010 6 -1006011 1 106011 3 30I want a add more row in there like: for each missing month. (month 3 and 5 for 6010 and month 2 for 6011)6010 1 -106010 2 -306010 3 -30 6010 4 -606010 5 -60 6010 6 -1006011 1 106011 2 10 6011 3 30 |
 |
|
|
drpkrupa
Yak Posting Veteran
74 Posts |
Posted - 2006-10-10 : 15:24:13
|
| Any update. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-10 : 15:57:55
|
| CROSS JOIN a tally table and there you have the missing months.Peter LarssonHelsingborg, Sweden |
 |
|
|
drpkrupa
Yak Posting Veteran
74 Posts |
Posted - 2006-10-10 : 16:19:22
|
| how can i find missing month. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-10 : 16:58:58
|
I told you. With a tally table.But since you didn't bother to investigate what that is, I figure this is homework.-- Prepare test dataDECLARE @Test TABLE ([Year] SMALLINT, [Account#] SMALLINT, Balance SMALLMONEY, [Month] TINYINT)INSERT @TestSELECT 2006, 6010, -10, 1 UNION ALLSELECT 2006, 6010, -20, 2 UNION ALLSELECT 2006, 6010, -30, 4 UNION ALLSELECT 2006, 6010, -40, 6 UNION ALLSELECT 2006, 6011, 10, 1 UNION ALLSELECT 2006, 6011, 20, 3-- Do the workSELECT d.[Account#], t.m [Month], (SELECT SUM(t2.Balance) FROM @Test t2 WHERE t2.[Year] = d.[Year] AND t2.[Account#] = d.[Account#] AND t2.[Month] <= t.m) RunningTotalFROM ( SELECT [Year], [Account#], 1 mi, MAX([Month]) ma FROM @Test GROUP BY [Year], [Account#] ) d INNER JOIN ( SELECT 1 m UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 ) t ON t.m BETWEEN d.mi AND d.maLEFT JOIN @Test t1 ON t1.[Year] = d.[Year] AND t1.[Month] = t.m AND t1.[Account#] = d.[Account#]ORDER BY d.[Year], d.[Account#], t.m Peter LarssonHelsingborg, Sweden |
 |
|
|
drpkrupa
Yak Posting Veteran
74 Posts |
Posted - 2006-10-11 : 11:08:03
|
| Thanks man. |
 |
|
|
|
|
|
|
|