Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have a following table :
Billing_Acct   Multiplier   Total
111               12          200
111               12          400
123               5           100
123               5           300
123               5           600
241               10          400


Now I want another column whose results perform calculations like (for same Billing_acct I want to add Total and than multiply that by multiplier )

Result would be something like that:
Billing_Acct   Multiplier   Total     Result
111               12          200      7200
111               12          400      7200
123               5           100      5000
123               5           300      5000
123               5           600      5000
241               10          400      4000


How to write a Sql Query for this ?
or How to do this task in Excel ?

Thanks in advance.
Posted

1 solution

If I understood the requirement correctly, this can be done in several ways, for example something like:
SQL
SELECT a.Billing_Acct,
       a.Multiplier,
       a.Total,
       c.multiplier * c.total as result
FROM TableName a,
     (SELECT b.Billing_Acct,
            AVG(b.Multiplier) AS multiplier,
            SUM(b.Total)      AS Total
     FROM  TableName b
     GROUP BY b.Billing_Acct) c
WHERE a.Billing_Acct = c.Billing_Acct;
 
Share this answer
 
v2
Comments
lovejeet0707 14-Aug-15 8:43am    
I guess we don't need to take the avg of multiplier....I can directly multiply with the multiplier....!
Thanks for the Help...Worked like a Magic :)
Wendelius 14-Aug-15 8:53am    
That's true, you can use the multiplier from the outer query. Glad if it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900