1

我试图在 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

任何帮助将不胜感激,我希望这比我之前的漫无边际更有意义!

4

2 回答 2

0

我认为您不想分组DatapointValue,因为这些是单独的值,而您正在寻找总和。

尝试更改GROUP BY为:

GROUP BY qb1.CompanyName, qb1.AssetName, qb1.Year
于 2012-09-05T14:16:35.823 回答
0
SUM(qb1.DatapointValue + IIF(ab2.DatapointValue <=5 AND qb2.DatapointValue > 20, qb3.DatapointValue, 0)) as NumberOfWellCompletions
...

GROUP BY qb1.CompanyName, qb1.AssetName, qb1.Year

您可以简化您的 SUM(好吧,不清楚您想要总结什么,但您可能会遇到括号问题)

您必须清楚是否需要检查<= 5>=5(问题中的文本和代码之间的差异)

您必须更改您的 GROUP BY

于 2012-09-05T14:17:05.480 回答