我试图在 Access 中编写一个计算,该计算涉及某些值的 SUM 加上 IF 语句的结果。
这是我正在处理的数据类型表(来自'CostBaseQuery)
CompanyName Year AssetName DatapointID Datapointname DatapointValue
CompanyA 2011 AssetA 4025 Active operated wells 129
CompanyA 2011 AssetA 4058 API gravity of oil 38.5563255
CompanyA 2011 AssetA 4032 Number of gas lift wells 70
CompanyA 2011 AssetA 4033 Number of wells with ESPs 0
CompanyA 2011 AssetA 4036 Wells with CO2 production 25
CompanyA 2011 AssetA 4035 Wells with H2S production 9
CompanyA 2011 AssetA 4038 Wells with high pressure 0
CompanyA 2011 AssetA 4037 Wells with high temperature 0
CompanyA 2011 AssetA 4034 Wells with scale production 42
CompanyA 2011 AssetA 4039 Wells with wax 0
计算需要SUM(DatapointValue) WHERE DatapointID IN (4032,4033,4035,4036,4037,4038,4039)
加上从 IF 语句返回的值
IF(API 油重 <=5 或 >20,使用 ActiveOperatedWells 的值,否则为 0)
我尝试使用以下方法对计算进行编码,但它在 SUM 的每次迭代中输出数据,而不是执行 SUM 计算,然后执行 IF 语句。
SELECT qb1.CompanyName, qb1.AssetName, qb1.Year,
(SUM(qb1.DatapointValue)
+ IIF(qb2.DatapointValue>=5,
IIF(qb2.DatapointValue<20, qb3.DatapointValue, 0), 0))
AS NumberOfWellCompletions
FROM (CostBaseQuery AS qb1
INNER JOIN CostBaseQuery AS qb2
ON qb1.CompanyYearAssetID=qb2.CompanyYearAssetID)
INNER JOIN CostBaseQuery AS qb3
ON qb1.CompanyYearAssetID=qb3.CompanyYearAssetID
WHERE qb1.DatapointID IN (4032,4033,3036,4035,4038,4037,4034,4039)
AND qb2.DatapointID=4058 AND qb3.DatapointID=4025
GROUP BY qb1.CompanyName, qb1.AssetName, qb1.Year,
qb1.DatapointValue, qb2.DatapointValue, qb3.DatapointValue
任何帮助将不胜感激,我希望这比我之前的漫无边际更有意义!