I have a view with fields:
typeId, type int
price, type decimal(15,4))
Price is calculated value.
What I would like is to add another calculated column (totalPrice) in the select statement that would sum all prices for records with same id giving me the result (assuming there are four rows in the view):
typeId price totalPrice
1 10,000 30,000
1 15,000 30,000
1 5,000 30,000
2 10,000 10,000
Thank you.
Assuming your existing view is named TheSummary. Rename it to x_TheSummary
Create a new view which bears the old name of the renamed view.
This way, your new view won’t have any breaking changes to apps that depends on old view name
By the way, since you are using SQL Server 2008, you can use the following windowing-query too, the query above works on non-windowing-capable RDBMS though.
It’s a lot simpler
@ivan-83 For a second, I thought SQL Server 2008 doesn’t support windowing on partition. I’m thinking too much of this construct(running total) that doesn’t work on SQL 2008, yet it works on SQL 2012.
The running total query doesn’t work on SQL 2008: http://www.sqlfiddle.com/#!3/d41d8/1539
And it now works on SQL 2012: http://www.sqlfiddle.com/#!6/d41d8/111
Outputs:
To sum it up, SQL Server 2012 not only it supports windowing on partition, it also supports windowing on row-by-row basis. SQL Server 2008 supports windowing on partition only. So @ivan-83 solutions works too. For this, I upvote your answer