2

嘿伙计们,我真的可以使用一些帮助来编码这个 IF 语句!我设法适应了 2 个条件,但不确定第 3 个(计算)如何适应。这是伪代码中的 calc;

IF Producer_Type = 'Oil', Unallocated OIL&GAS = UnallocatedLossesOIL
IF Producer_Type = 'Gas', Unallocated OIL&GAS = UnallocatedLossesGAS
ELSE Unallocated OIL GAS = UnallocatedLossesOIL(MPPOil ÷ (MPPOil + MPPGas)) + 
UnallocatedLossesGAS(MPPGas ÷ (MPPOil + MPPGas))

这是我的代码:

SELECT qb1.CompanyName, qb1.AssetName, qb1.Year, 
IIf(qb1.Producer_Type='Oil',qb1.DatapointValue, qb2.DatapointValue) AS UnallocatedOilGas
FROM PEBaseQuery AS qb1 INNER JOIN PEBaseQuery AS qb2 ON qb1.Assetname=qb2.AssetName
WHERE qb1.DatapointID=2026 And qb2.DatapointID=2027;

其中 2026 的 DatapointID 是 UnallocatedLossesOIL,而 2027 是 UnallocatedLossesGAS。计算需要参考 MPPOil 和 MPPGas 值(与特定公司/资产/年份相关,它们是来自两个单独查询的结果......任何帮助将不胜感激!抱歉,如果其中一些没有意义......

4

1 回答 1

1

嵌套if条件怎么样?我的意思是,如果可能的话,这应该有效:

SELECT qb1.CompanyName, qb1.AssetName, qb1.Year, 
IIf(qb1.Producer_Type='Oil', qb1.DatapointValue, 
  IIF(qb1.Producer_Type='Gas', qb2.DatapointValue,
   qb1.DatapointValue * (MPPOil / (MPPOil + MPPGas)) + 
   qb2.DatapointValue * (MPPGas / (MPPOil + MPPGas)) )) AS UnallocatedOilGas
FROM PEBaseQuery AS qb1 INNER JOIN PEBaseQuery AS qb2 ON qb1.Assetname=qb2.AssetName
WHERE qb1.DatapointID=2026 And qb2.DatapointID=2027;

关于 and 的值MPPGasMPPOil我不知道在哪里可以找到它们,所以让它们保持原样。我希望这行得通。

于 2012-08-26T17:36:55.700 回答