如何在SQL中编写此计算代码

时间:2012-09-04 11:44:59

标签: sql ms-access-2010

我试图在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的值,Else 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

任何帮助都会非常感激,我希望这比我以前的漫无边际更有意义!

1 个答案:

答案 0 :(得分: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。