I am trying to move some calculations out of Excel into my Access database but i am faced with the 'Query too complex' error when i use above 5 input values. Should i split the query up or is there a more efficient way of working around this?? Any help would be appreciated! Here is the code:
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
It looks like you need an aggregation query, instead of this complex one. E.g.
The only issue is multiplying to 1000000 for the first Data Point. You could try
IIF
for that:Also please try such 'crazy' query, with sub query for this particular DatapointID, without
IIF
:Update for 'Production Max Potential'. Please try following:
Example of calculation with substract logic. Updated with final crazy SQL. Please also note that I would go with application logic or stored procedures for such kind of things: