需要基于一张记录的最大状态值来获得一张表中所有记录的值之和

时间:2019-01-23 17:46:47

标签: sql sql-server

我有3张桌子:

CP有

FIELD_ID | Acreage | CROP_CLASS
001-0485 | 7.62536 |  RATOON
004-1156 | 9.64524 |  PLANTCANE
002-8512 | 3.69851 |  RATOON
005-1151 | 2.89748 |  RATOON

DT有

 farmer_bsi |parcel_id  |  crop_season    | tonnage_adjusted | parcel_status
859         |  001-8596 |       3         |     251.252      |    1
 78         |  002-8512 |       2         |      51.252      |    3
 89         |  004-1156 |       3         |     151.252      |    2
 89         |  004-1156 |       3         |     11.252       |    3
85          |  005-1151 |       3         |     10.85        |    3

农民

ASSN | farmer_bsi 
CPC | 859
BBZ | 85

我需要按作物类别和关联从DT表获得的最大宗地状态为3的所有宗地的作物类别,吨位调整后的总和和面积的总和。状态3表示已关闭。第一个表也有farmer_bsi

IE。从上面我需要渲染为

Closed    | SumofAcreage | SumofTonnage
PLANTCANE | 9.64524      | 162.504
RATOON    | 6.59599      |  62.102 




**Code i use to get results**


SELECT CROP_CLASS As Closed, SUM(ACREAGE) AS SumofAcreage, SUM(delivered)       AS SumofTonnage
FROM dbo.vCurrentParcelStatus 
WHERE CropSeasonsID = 3 AND CURRENTSTATUS = '3'
GROUP BY CROP_CLASS
ORDER BY CROP_CLASS ASC



**vCurrentParcelStatus**



SELECT     TOP (100) PERCENT dbo.CaneParcel.geoid, MAX(dbo.vCurrentParcelStatusHelp.CurrentStatus) AS CURRENTSTATUS, dbo.CaneParcel.ACREAGE, 
                  dbo.CaneParcel.VARIETY, dbo.CaneParcel.FARMER, dbo.CaneParcel.BRANCH, dbo.CaneParcel.DATE_PLANT, dbo.CaneParcel.CROP_CLASS, 
                  SUM(ROUND(dbo.vCurrentParcelStatusHelp.delivered, 1)) AS delivered, SUM(dbo.vCurrentParcelStatusHelp.number_deliveries) AS number_deliveries, 
                  dbo.CaneParcel.ASSN, dbo.CaneParcel.PRODUCTION, dbo.CaneParcel.REAPING_LE, dbo.vCurrentParcelStatusHelp.parcel_id, 
                  dbo.vCurrentParcelStatusHelp.crop_season, dbo.vCurrentParcelStatusHelp.CropSeasonsID
FROM         dbo.CaneParcel INNER JOIN
                  dbo.vCurrentParcelStatusHelp ON dbo.CaneParcel.FIELD_ID = dbo.vCurrentParcelStatusHelp.parcel_id
GROUP BY dbo.CaneParcel.geoid, dbo.CaneParcel.ACREAGE, dbo.CaneParcel.VARIETY, dbo.CaneParcel.FARMER, dbo.CaneParcel.BRANCH, dbo.CaneParcel.DATE_PLANT, 
                  dbo.CaneParcel.CROP_CLASS, dbo.vCurrentParcelStatusHelp.parcel_id, dbo.CaneParcel.ASSN, dbo.CaneParcel.PRODUCTION, dbo.CaneParcel.REAPING_LE, 
                  dbo.vCurrentParcelStatusHelp.crop_season, dbo.vCurrentParcelStatusHelp.CropSeasonsID
ORDER BY dbo.vCurrentParcelStatusHelp.parcel_id


**vCurrentParcelStatusHelp**


SELECT     TOP (100) PERCENT MAX(parcel_status) AS CurrentStatus, COUNT(tonnage) AS number_deliveries, SUM(tonnage_adjusted) AS delivered, parcel_id, 
                  farmer_owner_bsi, crop_season, CropSeasonsID
FROM         dbo.vDeliveryTons
GROUP BY parcel_id, farmer_owner_bsi, crop_season, CropSeasonsID
ORDER BY parcel_id

上面的前2个代码是视图,第一个在上面的其他代码中使用,最后是我用作上面的第三个代码的代码。但是,您的代码似乎无法复制我从代码中得到的内容。

根据上面的代码,我可以计算最大包裹状态,而根据我在代码中使用的其他代码,可以得出吨位和面积的总和。但是我似乎无法使用下面的代码来复制它。

2 个答案:

答案 0 :(得分:1)

这是一个简单的分组依据

Create Table #cp
(
Field_ID VarChar(15),
Acreage Float,
Crop_Class VarChar(15)
)
Insert Into #cp Values
('001-0485',7.62536,'RATOON'),
('004-1156',9.64524,'PLANTCANE'),
('002-8512',3.69851,'RATOON'),
('005-1151',2.89748,'RATOON')

Create Table #dt
(
farmer_bsi Int,
parcel_id VarChar(15),
crop_season Int,
tonnage_adjusted Float,
parcel_status Int
)
Insert Into #dt Values
(859,'001-8596',3,251.252,1),
(78,'002-8512',2,51.252,3),
(85,'004-1156',3,151.252,2),
(89,'004-1156',3,11.252,3),
(85,'005-1151',3,10.85,3)


Create Table #farmers
(
assn VarChar(5),
farmer_bsi Int
)
Insert Into #farmers Values
('CPC',859),
('BBZ',85)

查询:已更新为仅包含状态为3的宗地ID的联接。然后,应汇总至少一个记录状态为3的宗地的所有宗地记录

Select 
   #cp.Crop_Class As Closed, 
   Sum(#cp.Acreage) as SumOfTonnage,
   Sum(#dt.tonnage_adjusted) as SumOfTonnage
From 
   #cp Left Join
   #dt On #dt.parcel_id  = #cp.Field_ID Left Join
   #farmers f On f.farmer_bsi = #dt.farmer_bsi Join
   (Select Distinct #dt.parcel_id From #dt Where parcel_status = 3) c On c.parcel_id = #cp.Field_ID
Group By 
   #cp.Crop_Class

结果:

Closed      SumOfTonnage    SumOfTonnage
PLANTCANE   19.29048        162.504
RATOON      6.59599         62.102

答案 1 :(得分:1)

请尝试以下操作,由于您得到的结果不同,我不得不仔细检查脚本。

您提到只需要parcel_status = 3,但结果中似乎包含状态2。

如果需要2和3,则将联接上的“ = 3”部分交换为“ <> 1”,或者如果其中也可能存在其他值,则将“ IN(2,3)” >

此外,基于所需结果,除非您也希望每个农民获得结果,否则在此查询中我看不到Farmers表的任何需求,但这将是不同的报告。

    SELECT
    CP.CROP_CLASS AS Closed
    ,SUM(CP.Acreage) AS SumofAcreage
    ,SUM(DT.tonnage_adjusted ) AS SumofTonnage
    FROM CP
    INNER JOIN DT ON CP.FIELD_ID = DT.parcel_id AND parcel_status <= 3
    GROUP BY CROP_CLASS

我得到的结果与您的结果相同;

    Closed       SumOfAcreage      SumOfTonnage
    PLANTCANE    19.29048          162.504
    RATOON        6.59599           62.102