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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to do a Weighted Average!!!!

Author  Topic 

CoffeeAddict
Yak Posting Veteran

94 Posts

Posted - 2012-06-28 : 01:32:11
I'm totally new to doing calculations in T-SQL. I guess I'm wondering what is a weighted average and how do you do it in T-SQL for a field?

Here's an example of a calculated field I have after calling one of our UDFs. Now this has to be a weighted average also...no idea where to start to turn this into a weighted average.

So ultimately this UDF I call from my view, after I pass in some values returns a calculated value

Set @AverageCostG = ((@AvgFullYear_Rent * @Months) +
(@PrevYearRent * @PrevYearMonths))
/ @Term

so in my view I'm calling the UDF above to get back that @AverageCostG

CREATE View MyTestView
AS
select v.*, --TODO: get rid of *, that's just for testing, select actual field names
CalculateAvgRentG(d.GrossNet, d.BaseMonthlyRent, d.ILI, d.Increase, d.Term) as AverageRent_G,
....

from SomeOtherView v

Now I need to I guess make this AverageRent_G a weighted average somehow...

I'm sorry I'm not a BI Developer but I'm on a stupid BI project. Just need a bit of help the first time through this.

Do I need to know WHAT they want weighted or is it assumed that hey, it's obvious.. I do not know what I need to know in order to do the weighted average for these guys...like what specs I need if any from them other than this calculation I've created based off the UDF call. I mean do I have to do some crazy select join or something in addition to multiplying 2 fields and dividing by something to average it? How do I know what fields they are to be used int he weighted average and from where? I admit I'm totally new and lost to this.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-06-28 : 04:50:35
Weighted Median: http://www.sqltopia.com/?page_id=62
Weighted Average: http://weblogs.sqlteam.com/peterl/archive/2007/12/10/Cursor-is-really-faster-than-set-based-solution.aspx



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -