> I hate when I can't figure out why something didn't work
Look at this query:
SELECT ProductID, Sum(UnitsInStock * UnitPrice) As TotValue
FROM Products
GROUP BY ProductID
The first row is the fields in your grid (two fields), the last is the "total" row. You will group by product and sum the expression. You tried something like this:
SELECT
ProductID,
IIf(Discontinued, UnitPrice/2, UnitPrice) as Adjusted,
Sum(UnitsInStock * UnitPrice) As TotValue
FROM Products
GROUP BY ProductID
It doesn't matter whether you try to use 'Adjusted' in the expression or not. The query can no longer work.
Jet needs to know how to handle 'Adjusted'. Do you want to GROUP BY this expression? or do you want to perform an aggregation? You have only three ways out:
1) Grouping
SELECT
ProductID,
IIf(Discontinued, UnitPrice/2, UnitPrice) as Adjusted,
Sum(UnitsInStock * UnitPrice) As TotValue
FROM Products
GROUP BY ProductID, IIf(Discontinued, Price/2, Price)
But that isn't what you want: you no longer have one total per product. Every difference in 'Adjusted' creates a new row.
2) Aggregating
SELECT
ProductID,
Avg(IIf(Discontinued, UnitPrice/2, UnitPrice)) as Adjusted,
Sum(UnitsInStock * UnitPrice) As TotValue
FROM Products
GROUP BY ProductID
This works, but again isn't what you want. This shows an average price (or a total of prices, the minimum, the maximum), unusable in any further calculations...
3) Encapsulate
SELECT
ProductID,
Sum(UnitsInStock * IIf(Discontinued, UnitPrice/2, UnitPrice)) As TotValue
FROM Products
GROUP BY ProductID
Does that make sense?
(°v°)