2012-08-26 57 views
2

嗨,我真的可以使用一些幫助來編寫這個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值(與特定公司/資產/年相關,這是兩個單獨查詢的結果...任何幫助將非常感謝!對不起,如果這一些沒有意義..

+0

是'UnallocatedLossesOIL'函數,還是你inte將其與括號相鄰以暗示增殖? –

回答

1

如何嵌套如果條件我的意思是,如果多數民衆贊成可能的,這應該工作:?

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; 

關於MPPGasMPPOil的價值觀,我不知道在哪裏可以找到他們,所以離開了他們,因爲他們是 我希望這可以工作。

+0

+1我認爲'case'可能更清晰,不確定是否Access支持 – Andomar

+0

真的很感謝幫助,它的工作原理是無誤的:) – Magda

+0

歡迎您。 :) –