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 |
bahamaer
Starting Member
1 Post |
Posted - 2008-05-05 : 17:41:02
|
Currently trying to create a running total on a table that holds account numbers, amounts, and groupings that the amounts are assigned to. I have been able to do this, but now I want to perform calculations on the running totals for each account number. I am only able to do this if there is an account number in the where clause. This is what I have so far. Everything works fine, until I have to perform the calculations beginning with the code in red, from what I see I just need to be able to group all of that by account number, and reset the variables for each account number. Any suggestions?DECLARE @totBalance TABLE( account_number bigINT, tbb MONEY, cbalance money, rt1 money, rt2 money, rt3 money, calbalance money )insert into @totbalance (table1.account_number, tbb, cbalance, rt1, rt2, rt3)SELECT table1.account_number, total_beginning_balance, current_balance, sum (CASE WHEN table1.transaction_logic_module = 1 THEN table1.amount ELSE 0 END) RunningTotal1, sum (CASE WHEN table1.transaction_logic_module = 30 THEN table1.amount ELSE 0 END) RunningTotal2, SUM(CASE WHEN table1.transaction_logic_module = 5 OR table1.transaction_logic_module = 7 THEN table1.amount ELSE 0 END) RunningTotal5_6 FROM table1 INNER JOIN table2 ON table1.account_number = table1.account_number AND table1.billing_cycle_date = table2.date_current_statement AND table1.organization_number = table2.organization_number AND table1.logo = table2.logoGROUP BY table1.account_number, total_beginning_balance, current_balanceupdate @totbalanceset @rtotal1 = rt1, @rtotal2 = rt2, @rtotal3 = rt3, @totbegbalance = tbbdeclare @balance1 moneyset @balance1=0.0declare @balance2 moneyset @balance2=0.0declare @balance3 moneyset @balance3=0.0set @balance1 = case when @rtotal2 > 0 then @totbegbalance - @rtotal2 endset @balance2 = case when @rtotal3 > 0 then @balance1 + @rtotal3 endset @balance3 = case when @rtotal1 > 0 then @balance2 + @rtotal1 endupdate @totbalanceset @balance = calbalance = @balance3Select * from @totbalanceorder by account_number |
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2008-05-05 : 21:31:49
|
quote: update @totbalanceset @rtotal1 = rt1, @rtotal2 = rt2, @rtotal3 = rt3, @totbegbalance = tbb
What are @rtotal1, @rtotal2,@rtotal3 and @totbegbalance??You don't have a from clause in your update statement. Maybe you're trying to do something like thisdeclare @balance1 moneyset @balance1=0.0declare @balance2 moneyset @balance2=0.0declare @balance3 moneyset @balance3=0.0select @balance1 = case when @rtotal2 > 0 then @totbegbalance - @rtotal2 end, @balance2 = case when @rtotal3 > 0 then @balance1 + @rtotal3 end, @balance3 = case when @rtotal1 > 0 then @balance2 + @rtotal1 endFrom @totbalanceWhere .....? Anyway, can you list your input and your desired output?An infinite universe is the ultimate cartesian product. |
 |
|
|
|
|
|
|