# weighted average

I have the following table

A  B   ID
1  8    s
4  9    x
5  10   m
6  11  n
7  12   o

I want to calculated weighte average as follows
only for IDs  s and x i want to find weighted average 1/sum(all Bs) and 4/sum of all Bs

what is the query for this?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
not really sure what you exactly are looking for, but give this a try.

SELECT tweight.ID, IIf([ID]="S",1/Sum(B),0) AS Expr1, IIf([ID]="X",4/Sum(B),0) AS Expr2
FROM tweight
GROUP BY tweight.ID
HAVING (((tweight.ID) In ("s","x")));
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
If I understand you correctly, then you want to weight average the table based on the ID based upon the sum of the values in Column B.  For "s" you want it to weight with a numerator of 1 and for "x" you want a numerator of 4.  All others I am assuming you want as the sum of the value of B for that given ID.  The query is below.  To add more IDs and their weights you can union them on WEIGHT_AMT.

``````SELECT MT.*, CASE WHEN WEIGHT_AMT.ID IS NULL THEN POWER(SUM_OF_B.SUM_OF_B, 2) ELSE WEIGHT_AMT.WEIGHT_AMT END / CAST(SUM_OF_B.SUM_OF_B AS decimal) AS WEIGHTED_AVERAGE
FROM
MyTable MT
LEFT JOIN (SELECT 's' AS ID, 1 AS WEIGHT_AMT UNION SELECT 'x', 4) WEIGHT_AMT ON MT.ID = WEIGHT_AMT.ID
OUTER APPLY (SELECT SUM(B) AS SUM_OF_B FROM MyTable WHERE ID = MT.ID) SUM_OF_B
``````

Rey has got a great point in his writing.  He supplied Access SQL, I supplied SQL Server SQL.  Question was in both categories and indeterminate as to where the query is needed.
0
Commented:
With just this simple dataset, Rey's recommendation would be fine, but with a more exhaustive recordset, I think something like the following would be quicker:

SELECT A.ID, A.[A]/B.[Total]
FROM yourTable as A, (SELECT SUM(B) as Total FROM yourTable) as B
WHERE A.ID IN ("s", "x")
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.