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 eg 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
select
tc.ContractID
,tc.PlotNumber
,tc.TotalArea
,tc.TotalArea *tcTotals.Cost_Per_Area as [Notary_Cost]
,tc.LandTax
,tc.OtherTaxes
from
tblContracts tc
inner join
(
select
ContractID
,SUM(notaryCosts) as [sum_NotaryCost]
,SUM(TotalArea) as [sum_TotalArea]
,SUM(notaryCosts)/SUM(TotalArea) as [Cost_Per_Area]
from
tblContracts
group by
ContractID
) tcTotals
on
tc.ContractID =tcTotals.ContractID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.