Im trying to code a calculation in Access that involves the SUM of some values plus the result of an IF statement.
Here is a table of the kind of data I’m dealing with (from ‘CostBaseQuery)
CompanyName Year AssetName DatapointID Datapointname DatapointValue
CompanyA 2011 AssetA 4025 Active operated wells 129
CompanyA 2011 AssetA 4058 API gravity of oil 38.5563255
CompanyA 2011 AssetA 4032 Number of gas lift wells 70
CompanyA 2011 AssetA 4033 Number of wells with ESPs 0
CompanyA 2011 AssetA 4036 Wells with CO2 production 25
CompanyA 2011 AssetA 4035 Wells with H2S production 9
CompanyA 2011 AssetA 4038 Wells with high pressure 0
CompanyA 2011 AssetA 4037 Wells with high temperature 0
CompanyA 2011 AssetA 4034 Wells with scale production 42
CompanyA 2011 AssetA 4039 Wells with wax 0
The calculation needs to SUM(DatapointValue) WHERE DatapointID IN (4032,4033,4035,4036,4037,4038,4039)
PLUS the value returned from the IF Statement
IF (API gravity of oil <=5 or >20, Use the value of ActiveOperatedWells, Else 0)
I tried to use the following method to code the calculation but it outputs data on each iteration of the SUM rather than carrying out the SUM calculation and THEN the IF statement.
SELECT qb1.CompanyName, qb1.AssetName, qb1.Year,
(SUM(qb1.DatapointValue)
+ IIF(qb2.DatapointValue>=5,
IIF(qb2.DatapointValue<20, qb3.DatapointValue, 0), 0))
AS NumberOfWellCompletions
FROM (CostBaseQuery AS qb1
INNER JOIN CostBaseQuery AS qb2
ON qb1.CompanyYearAssetID=qb2.CompanyYearAssetID)
INNER JOIN CostBaseQuery AS qb3
ON qb1.CompanyYearAssetID=qb3.CompanyYearAssetID
WHERE qb1.DatapointID IN (4032,4033,3036,4035,4038,4037,4034,4039)
AND qb2.DatapointID=4058 AND qb3.DatapointID=4025
GROUP BY qb1.CompanyName, qb1.AssetName, qb1.Year,
qb1.DatapointValue, qb2.DatapointValue, qb3.DatapointValue
Any help would be much appreciated and I hope this makes more sense than my previous rambling!
You can simplify your SUM (well, not clear about what you want to sum, but you may have a problem of brackets)
You must be clear if you need to check
<= 5or>=5(difference between text and code in your question)You must change your GROUP BY