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 |
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-03-04 : 05:44:36
|
Hi Good people of ST.I consult for a bank. I want to get average balances of about 2 millions accounts from a transaction table holding end of day balances.see code withing function belowquote: while @tempDate <= @endDate begin begin select @tempBal = value_date_bal from dbo.SRC_EAB where acid = @cAcid and @tempDate >= COALESCE(eod_date, eod_date) and end_eod_date >= @tempDate OPTION (FORCE ORDER) SELECT @SWV_no_data_err = @@ROWCOUNT IF (@SWV_no_data_err = 0) SET @tempBal = 0.00 end if @tempBal <= 0.00 SET @debitBal = @debitBal+@tempBal else SET @creditBal = @creditBal+@tempBal SET @tempDate = dateadd(day,1,@tempDate) end
How can I put this inline (going back and forth the function for every records) or how can I optimize these say my only option is to use function.Many thanks people. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-04 : 06:33:59
|
don't need the while loop. Do it the set wayselect @debitBal = sum(case when value_date_bal <= 0 then value_date_bal else 0 end), @creditBal = sum(case when value_date_bal > 0 then value_date_bal else 0 end)from dbo.SRC_EABwhere acid = @cAcidand @tempDate >= COALESCE(eod_date, eod_date)and end_eod_date >= @beginDateand end_eod_date <= @endDate KH[spoiler]Time is always against us[/spoiler] |
 |
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-03-04 : 07:26:34
|
@KH, am very greatful boss. Yes, I know am suppose to go SET way, but my brain can't form that way. Mind you direct me to any link that simplifies SET way of TSQL.I sign for fame not for shame but all the same, I sign my name. |
 |
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-03-04 : 10:28:20
|
@KH, the above seems not to work for me. These whole exercise is to get average balance for per month.I will need to get balance of everyday and divide by number of days in the month.I sign for fame not for shame but all the same, I sign my name. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-04 : 10:44:41
|
depending on your requirementjust change the sum() to avg()or sum(....) / datediff(day, beginDate, endDate) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|