Solved

calculating a weighted average in SQL, SSRS

Posted on 2010-11-14
7
3,951 Views
Last Modified: 2012-05-10
Hello Experts,

Does anyone know how to calculate a weighted average in SQL?

First I get the average of something: COUNT(transactions)/Fee = Average
Then to get the weighted average I would have to total the above values of 'Average' for the three different transactions types we use, and then divide that by the total count.

I cannot create a calculated field because in SSRS you can't use aggregate functions in a calc field. Also, in SSRS, I have tried creating an expression:

=(SUM((Fields!CRDTAMNT.Value/COUNT(Fields!Trans.Value))* COUNT(Fields!Trans.Value)))/COUNT(Fields!Trans.Value)

and I keep getting errors about nested aggregate functions.

So first of all, should I do this in my query or somewhere in SSRS?
Second of all, what is the code/syntax to achieve this?

Thanks.

0
Comment
Question by:erp1022
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 34131907
You have the value and the weight in each row or you know the weights.
in either case you would first sum the weights into a variable and then use the formula of wight*value/sum(weights)

select @total_weight=sum(weights) from table;
select @weighted_average+=sum(weights*values/@total_weight) from table;

have not tried it such that not sure whether it is even valid.
 
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 200 total points
ID: 34131921
I would create the weighted average in your underlying query.

This article shows how to calculate a weighted average; while it is done for Access, the logic for SQL Server is much the same, except you would use CASE instead of IIf:

SELECT {group by columns if desired,} (CASE WHEN MIN([WeightColumn]) >=0 AND
    MAX([WeightColumn]) > 0 THEN SUM([WeightColumn] * [ValueColumn]) ELSE NULL END) /
    SUM([WeightColumn]) AS WtdAvg
FROM {tables}
{GROUP BY {group by columns}}

Open in new window


Patrick
0
 

Author Comment

by:erp1022
ID: 34132408
Okay now I'm really confused.

So if my weighted average is based on a count of transactions [COUNT(Trans)] * an amount, how would I write my code in SQL?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34132429
Perhaps it would be best if you could work up some sample source data, and then the output you would expect given that input.
0
 

Author Comment

by:erp1022
ID: 34132779
Please see attachment...
Weighted-Average.xls
0
 
LVL 41

Expert Comment

by:ralmada
ID: 34136671
try this query
select 	transtype,
	count(items),
	sum(amount),
	sum(amount * items) * 1.0 / sum(amount) as weight_avg
from yourtable
group by transtype
with rollup

Open in new window

0
 
LVL 41

Accepted Solution

by:
ralmada earned 300 total points
ID: 34136969
sorry actually it should be
select 	transtype,
	count(items),
	sum(amount),
	sum(amount) * count(items) * 1.0 / sum(amount) as weight_avg
from yourtable
group by transtype
with rollup

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 37
SSIS with VPN COnnection 2 77
Help in Bulk Insert 9 35
SQL Server Error 21 8 25
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question