从多个表

时间:2016-02-17 05:45:54

标签: sql-server

我目前正致力于为本地公司开发基于Web的应用程序。(我在大学的高级项目)我终于得到了我想要的数据,除了一张表。这是一种用水跟踪系统,它依赖于总分配。因此,正如您在链接中提供的图片中所看到的,我有一个包含该公司管理的所有MapTaxLots的大表。每个MapTaxLot都可以有多种类型的土地。示例)AcresAmountKID,AcresAmountKBID,AcresAmountWarren **此外,一个客户可以拥有许多MapTaxLots。我需要做的是填充另一个表,其中总分配在每个MapTaxLot中采用每种类型的英亩,并将其与所提供图像中的小表中的相应乘数相乘。完成所有这些计算后,我需要为每个客户添加所有计算,以便将总分配添加到空表中。

TL; DR:我需要将每个MapTaxLot的小表中的AcresAmountKID乘以小表乘以KID。然后,我为剩余的AcreTypes执行此操作。然后,我需要将所有值添加到每个客户的一个总和(分配)中,并将该计算插入到空表中。如果这很难理解,我很抱歉,但这个问题非常独特,而且我从未用这种方式从不同的表中做过计算。

http://imgur.com/a/f8Oww

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,这是您想要的查询,这对于MSSQL Server

insert into Allotment
select customerID, sum(
(select AcresAmountKID from land  where MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='KID')) + 
(select AcresAmountKBID from land where  MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='KBID')) +
(select AcresAmountWarren  from land where  MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='WARREN')) + 
(select AcresAmountGroupE  from land where  MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='GROUPE')) + 
(select AcresAmountRent  from land where  MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='RENT')) +
(select AcresAmountCalD  from land where  MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='CALD')) ), TotalAllocation
sum((select AcresAmountKID  from land where  MapTaxID = ld.MapTaxID ) * (select MultiplierAmount from AcreMultiplier where AcreTypeID='KID')) remainingAcreTypes, 
Null 
from Land ld where group by customerID
相关问题