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 |
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-10-06 : 12:17:00
|
All,I have Table ASH with the following field.SaleDate(smalldatetime)AggID (nvarchar)ProdID (nvarchar)Price (float)cost (float)units (bigInt)The table represnts price & cost of a particular product, that belong to a particular AggID.I have the following records.SaleDate AggID ProdID Price Cost Units2006-10-01 A123 123 1.90 1.70 102006-10-01 A123 456 1.90 1.70 102006-10-01 A123 789 1.80 1.65 102006-10-01 A999 999 2.80 2.25 102006-10-01 A999 888 2.70 2.20 20Now I want to get the Price & Cost at AggID level.The formula for that isAggPrice = (sum(price*units)) / (sum(units))AggCost = (sum(cost*units)) / (sum(units))So the calculation for AggID= A123 would be:AggPrice = ((1.90*10)+(1.90*10)+(1.80*15)) / (10+10+15)AggPrice = 1.8571AggCost = ((1.70*10)+(1.70*10)+(1.65*15)) / (10+10+15AggCost = 1.6785Now I want to have a query which can process this calulation & can show the results as Follows per AggIDAggID AggPrice AggCostA123 1.8571 1.6785Remember i may have more AggID's.Can someone please help me on this. Any help would be appreciated.Thanks a million |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-06 : 13:39:14
|
[code]Select AggID, AggPrice = (sum(price*units)) / (sum(units)) , AggCost = (sum(cost*units)) / (sum(units))from ASHgroup by AggIDorder by AggID[/code]CODO ERGO SUM |
 |
|
|
|
|