I am trying to run a query that will calculate "TOTAL COST". The current code I have is:
SELECT [Main Query].SaltInfo_ID, Sum([Main Query].Tons) AS [Total Of Tons], Sum([Main Query].BlastTons) AS [Total Of BlastTons], Sum([Main Query].LiqGal) AS [Total Of LiqGal], Sum([Main Query].LiqBlastGal) AS [Total Of LiqBlastGal], ((Nz([Total Of Tons])+Nz([Total of BlastTons]))*66.63)+((Nz([Total Of LiqGal])+Nz([Total of LiqBlastGal]))*0.182) AS [TOTAL COST]
FROM [Main Query]
WHERE ((([Main Query].EventDate) Is Not Null))
GROUP BY [Main Query].SaltInfo_ID;
This works just fine; however I need to change the calculated [TOTAL COST] to calculate values based on the [EventDate]. If the event date was before 7/31/2016, the Tons and BlastTons cost is $66.63 and the LiqGal and LiqBlastGal is $0.182. After 7/31/2016, the cost changed to $50.40 and $0.1594 respectively. Can you please help with the proper coding so that this will calculate properly? I have tried this, and it didn't work:
SELECT [Main Query].SaltInfo_ID, Sum([Main Query].Tons) AS [Total Of Tons], Sum([Main Query].BlastTons) AS [Total Of BlastTons], Sum([Main Query].LiqGal) AS [Total Of LiqGal], Sum([Main Query].LiqBlastGal) AS [Total Of LiqBlastGal], IIf([Main Query].[EventDate]<#7/31/2016#,((Nz([Total Of Tons],0)+Nz([Total of BlastTons],0))*66.63)+((Nz([Total Of LiqGal],0)+Nz([Total of LiqBlastGal],0))*0.182),((Nz([Total Of Tons],0)+Nz([Total of BlastTons],0))*50.4)+((Nz([Total Of LiqGal],0)+Nz([Total of LiqBlastGal],0))*0.1594)) AS [TOTAL COST]
FROM [Main Query]
WHERE ((([Main Query].EventDate) Is Not Null))
GROUP BY [Main Query].SaltInfo_ID;
When I try to run the query, I get the error: "You tried to execute a query that does not include the specified expression 'IIf([Main Query].[EventDate]<#7/31/2016#,((Nz([Total Of Tons],0)+Nz([Total of BlastTons],0))*66.63)+((Nz([Total Of LiqGal],0)+Nz([Total of LiqBlastGal],0))*0.182),((Nz([Total Of Tons],0)+Nz([Total of BlastTons],0))*50.4)+((Nz([Total Of LiqGal],0)+Nz([Total of Li' as part of an aggregate function."
What I am doing wrong??? Any help would be greatly appreciated!