0

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

1 回答 1

1

这个问题对其他 SO 用户并没有真正有用,并且您没有说明您的实际问题,但是如果我以我喜欢的方式缩进您的查询,就像这样,

SELECT
              b.CompanyName
            , b.AssetName
            , b.Year
            , (
                b.CalculationResult + 
                mp.CalculationResult
              ) AS NumberOfWellCompletions

    FROM
        (
            SELECT 
                          CompanyName
                        , AssetName
                        , Year
                        , SUM(q1.DatapointValue) AS CalculationResult 
                FROM
                        CostBaseQuery AS q1 
                WHERE
                        DatapointID in (
                            4032,
                            4033,
                            3036,
                            4035,
                            4038,
                            4037,
                            4034,
                            4039) 
                GROUP BY
                          CompanyName
                        , AssetName
                        , Year
        ) AS b 
    LEFT JOIN 
        (
            SELECT
                          CompanyName
                        , AssetName
                        , Year
                        , (
                            SUM(
                                IIF(
                                    qb2.DatapointValue>=5, 
                                    IIF(
                                        qb2.DatapointValue<20, 
                                        qb3.DatapointValue,
                                        0),
                                    0)
                          ) 
                FROM
                        CostBaseQuery qb3                   
                WHERE
                        qb3.CompanyName = qb2.CompanyName
                    AND 
                        qb3.AssetName = qb2.AssetName
                    AND
                        qb3.Year = qb2.Year 
                    AND
                        qb3.DatapointID = 4025                   
                GROUP BY 
                          CompanyName
                        , AssetName
                        , Year
        )
    // What is going on here, this doesn't make sense.
    ) AS CalculationResult 
FROM 
          CostBaseQuery AS qb2 
WHERE 
          DatapointID = 4058 
GROUP BY
            CompanyName
          , AssetName
          , Year
 ) AS mp 
    ON 
            (b.CompanyName = mp.CompanyName)
        AND 
            (b.AssetName = mp.AssetName)
        AND
            (b.Year = mp.Year);

我可以看到你的括号数量错误,所以就像 Access Parser 说你有语法错误一样。

如果我倾向于为您编写查询,我将需要有关您的数据库 shcema 的更多信息。但是,您应该尝试编写一些可以解析的 SQL。

于 2012-09-04T16:33:22.047 回答