2012-08-23 211 views
5

我想將Excel中的一些計算移到我的Access數據庫中,但是當我使用上面的5個輸入值時,我遇到了'Query too complex'錯誤。我應該分開查詢還是有一個更有效的方法來解決這個問題?任何幫助,將不勝感激!下面是代碼:查詢太複雜

SELECT qb1.CompanyName, qb1.Assetname, qb1.Year, 
     ((qb1.DatapointValue*1000000)+qb2.DatapointValue+ 
     qb3.DatapointValue+qb4.DatapointValue+qb5.DatapointValue+ 
     qb6.DatapointValue) AS MPPOilRevised 

FROM ((((((PEBaseQuery AS qb1 
INNER JOIN PEBaseQuery AS qb2 ON qb1.AssetName=qb2.AssetName) 
INNER JOIN PEBaseQuery AS qb3 ON qb1.AssetName=qb3.AssetName) 
INNER JOIN PEBaseQuery AS qb4 ON qb1.AssetName=qb4.AssetName) 
INNER JOIN PEBaseQuery AS qb5 ON qb1.AssetName=qb5.AssetName) 
INNER JOIN PEBaseQuery AS qb6 ON qb1.AssetName=qb6.AssetName)) 

WHERE qb1.DatapointID=2003 And qb2.DatapointID=2032 
     And qb3.DatapointID=2034 And qb4.DatapointID=2042 
     And qb5.DatapointID=2036 And qb6.DatapointID=2030; 

CompanyName, Year, AssetName, DatapointID, DatapointName, DatapointValue 
CompanyA, 2011, CAAsset1, 2005,  OIL,    170883.740972926 
CompanyA, 2011, CAAsset1, 2032,  Wellwork,  615913 
CompanyA, 2011, CAAsset1, 2034,  Annual shutdown, 0 
CompanyA, 2011, CAAsset1, 2042,  Export,   0 
CompanyA, 2011, CAAsset1, 2036,  Plant,   958387 
CompanyA, 2011, CAAsset1, 2030,  Reservoir,  2394231 
+0

嘗試不上不下'qbX.DatapointValue'操作數爲'(SELECT qbX.DatapointValue FROM ... WHERE ... 。)AS ExprX'。 – EthanB

+0

一些單獨的問題..爲什麼你需要這個乘數爲1000000的第一個DatapointValue?它看起來很奇怪,因爲這些值來自一列。我認爲如果沒有它,就會讓整個查詢變得更簡單。 – udalmik

+0

乘以百萬是因爲我需要每百萬桶油的價值 – Magda

回答

3

它看起來像你需要的不是這種複雜的聚合查詢。例如。

select companyName, assetName, year, 
    Sum(DatapointValue) as MPPOilRevised 
from PEBaseQuery 
where DatapointID in (2032, 2034, 2042, 2036) 
group by companyName, assetName, year 

唯一的問題是第一個數據點乘以1000000。你可以嘗試IIF爲:

select companyName, assetName, year, 
    Sum(IIf(DatapointID=2003,DatapointValue*1000000,DatapointValue)) as MPPOilRevised 
from PEBaseQuery 
where DatapointID in (2032, 2034, 2042, 2036) 
group by companyName, assetName, year 

另外,請嘗試這種「瘋狂」的查詢,有子查詢這個特定DatapointID,沒有IIF

select companyName, assetName, year, SUM(DatapointValue) 
     + (select SUM(DatapointValue * 1000000) from PEBaseQuery q2 
      where q2.companyName = q1.companyName 
       and q2.assetName= q1.assetName 
       and q2.year= q1.year 
       and q2.DatapointID = 2003 
      group by companyName, assetName, year) 
    from PEBaseQuery q1 
    where DatapointID in (2032, 2034, 2042, 2036) 
    group by companyName, assetName, year 

更新「生產最大潛力」。請嘗試以下操作:

select b.companyName, b.assetName, IIf(b.calculationResult > mp.calculationResult,b.calculationResult,mp.calculationResult) as MPPOilRevised 
from 
    (select companyName, assetName, year, Sum(IIf(DatapointID=2003,DatapointValue*1000000,DatapointValue)) as calculationResult 
     from PEBaseQuery 
     where DatapointID in (2032, 2034, 2042, 2036) 
     group by companyName, assetName, year) b --Base 
    left join 
    (select companyName, assetName, year, 
     Sum(DatapointValue) as calculationResult 
     from PEBaseQuery 
     where DatapointID = 2218 
     group by companyName, assetName, year) mp -- Max Potential 
    on b.companyName= mp.companyName 
     and b.assetName = mp.assetName 
     and b.year = mp.year 

帶減法邏輯的計算示例。 更新與最後的瘋狂SQL。另請注意,我將與應用邏輯或存儲過程去這樣那樣的事情:

select b.companyName, b.assetName, IIf(b.calculationResult > mp.calculationResult,b.calculationResult,mp.calculationResult) as MPPOilRevised 
from 
    (select companyName, assetName, year, SUM(DatapointValue) 
     + (select SUM(DatapointValue * 1000000) from PEBaseQuery q2 
      where q2.companyName = q1.companyName 
       and q2.assetName= q1.assetName 
       and q2.year= q1.year 
       and q2.DatapointID = 2003 
      group by companyName, assetName, year) 
     - (select SUM(DatapointValue) from PEBaseQuery q2 
      where q2.companyName = q1.companyName 
       and q2.assetName= q1.assetName 
       and q2.year= q1.year 
       and q2.DatapointID = 2029 
      group by companyName, assetName, year) 
    from PEBaseQuery q1 
    where DatapointID in (2032, 2034, 2042, 2036) 
    group by companyName, assetName, year) b --Base 
    left join 
    (select companyName, assetName, year, 
     Sum(DatapointValue) as calculationResult 
     from PEBaseQuery 
     where DatapointID = 2218 
     group by companyName, assetName, year) mp -- Max Potential 
    on b.companyName= mp.companyName 
     and b.assetName = mp.assetName 
     and b.year = mp.year 
+0

好點,我沒有看到! – Fionnuala

+0

我估計你可以在IIf中加入乘法運算。 – Fionnuala

+0

試圖更新)將稍後檢查 – udalmik