嗨,我真的可以使用一些幫助來編寫這個IF語句!我設法滿足2個條件,不知道3rd(計算)如何適合。這是僞代碼中的計算;帶有3種替代方法的SQL IF語句
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值(與特定公司/資產/年相關,這是兩個單獨查詢的結果...任何幫助將非常感謝!對不起,如果這一些沒有意義..
是'UnallocatedLossesOIL'函數,還是你inte將其與括號相鄰以暗示增殖? –