Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I am trying to calculate the amount of a total price a detail record represents. I have a header and a detail table. On the header is a total and on the detail is the cost for each item. I need to total the cost and calculate the percent a detail is of the total cost and then take that against the total selling price. What amount of the selling price each piece contributed.I can get the individual selects to work but am not sure how to join them or if I need to use a cursor.Select TransId, ItemId, ItemPrice, ComponentId, (ItemPrice * (Select ComponentCost/(Select TransId, ComponentId, Sum(ComponentCost) from Detail Group By TransId, ItemId)) AS ComponentCost From Headervmon
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-11-09 : 15:40:30
Since you don't bother to include DDL, here is what you get.
Select h.TransId, h.ItemId, h.ItemPrice, h.ComponentId, h.ItemPrice * h.ComponentCost / q.x AS ComponentCostFrom Header hinner join ( Select TransId, Sum(ComponentCost) x from Detail Group By TransId, ItemId ) q on q.transid = h.transid