I need your help in the following situation if is possible:
I use Sql Server 2008 R2 and I have this table:
create table tblContracts
([ContractID] [int] NOT NULL,
[PlotNumber] [nvarchar](150) NOT NULL,
[TotalArea] [numeric](12, 0) NULL,
[NotaryCosts] [numeric](12, 2) NULL,
[LandTax] [numeric](12, 2) NULL,
[OtherTaxes] [numeric](12, 2) NULL)
In this table I could have several records with the same ContractID but with different PlotNumber.
I want to dinamically split the costs for each PlotNumber depending on TotalArea where ContractID is the same.
For e.g. if I have next records:
1. ContractID=1,PlotNumber=1,TotalArea=100,NotaryCosts=300,LandTax=10,OtherTaxes=0 and
2. ContractID=1,PlotNumber=2,TotalArea=200,NotaryCosts=?,LandTax=20,OtherTaxes=0
The formula for NotaryCosts should update itself dynamically according to the next formula:
NotaryCosts(for each PlotNumber)=sum(NotaryCosts with the same ContractID)/sum(TotalArea with the same ContractID)*TotalArea(for the specific PlotNumber)
It is possible to do this somehow?
Thank you in advance!
I pretty much agree with Thomas, I think the table design needs some work as well as how data is being entered into the system. At any rate here is a query that could be used as view to give you the calculated total you are look for or could be used in an Update statement. To do this automatically this logic would need to be placed in a trigger