Solved Why Some Calculated Fields Will Not Work In A Query? (1 Viewer)

A plus one for @Mike Krailo option.
Personally, I wouldn't get into the habit of trying to reference calculated fields in the query by their alias in the query they are created in.
Other RDBMS don't let you do it, so if you ever try and use the same query structure it will fail.

I've had to convert a few of these abominations into T-SQL (SQL Server) and it's really painful.
 
This is your only option to get the effect you were going for.
That is not true. As long as the OP drops query1 from the sql it works.
In this case there is no reason to duplicate the alias field.
Code:
SELECT length,
       width,
       depth,
       length * width AS AREA,
       area * depth   AS VOLUME
FROM   table1;

Now if you want to sort or filter on area or volume you will have to duplicate the calculations for the area. This is because of query order of operations. The Select is applied after the Oder By.

Code:
1. FROM
2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. WITH CUBE/ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP

 
Last edited:
Am I wrong here (usually am) - It seems in a query a calculated field (residing in that query) will require a subquery to be used if you reference the query name in the formula:
if the referenced name only appears in ONE source (table or query), no qualification is required. If it appears in more than one source, then it must be qualified by a source. The NameAutoCorrect feature does NOT properly correct field names embedded in calculations so you will almost always run into errors if you change column/table/object names and are relying on NameAutoCorrect to fix up and propagate the changes.
 
That is not true. As long as the OP drops query1 from the sql it works.
In this case there is no reason to duplicate the alias field.
Code:
SELECT length,
       width,
       depth,
       length * width AS AREA,
       area * depth   AS VOLUME
FROM   table1;

Now if you want to filter on the Area or Volume field you will have to duplicate the calculations. This is because of query order of operations. The alias is applied after the filter.

I didn't word that correctly, was focused on the use of Quer1 in the Quer1 itself which is not possible. Referencing aliases in the same query is possible. My mistake.
 
Referencing aliases in the same query is possible.
This is also confusingly worded.

Query1 is the name of the query itself, and yes you are right that you can't reference the QueryDef's own name within the query itself.

But it is not an alias.
 
This is also confusingly worded.

Query1 is the name of the query itself, and yes you are right that you can't reference the QueryDef's own name within the query itself.

But it is not an alias.
What was confusing about it. You CAN reference the value of an alias in a query as was just demonstrated in the last few posts.

Wait, I may have updated my post after you read the initial one. Maybe that's where the confusion came in.
 
As already stated, can reference alias (or constructed) field name in another calc but don't include the table or query name. So an addition to the SQL Mike shows:

SELECT Table1.*, Length*Width AS AREA, AREA*Depth AS VOLUME FROM Table1;
 
In the list of operations you post immediately after this SELECT (operation 8) comes before ORDER BY (operation 10)
I should have said that the Where comes before the select, which should explain the Where. Not sure the reason for the sort, but it is the same and you cannot use the alias in the sort. In either case you get a Parameter Prompt because it does not know about the alias when that occurs

So you have to duplicate the formula in the Order By (or the where). So you can do this
Code:
SELECT table1.length,
       table1.width,
       table1.depth,
       [table1].[length] * [table1].[width] AS AREA
FROM   table1
ORDER  BY [table1].[length] * [table1].[width] DESC;

Cannot do this
Code:
SELECT table1.length,
       table1.width,
       table1.depth,
       [table1].[length] * [table1].[width] AS AREA
FROM   table1
ORDER  BY [AREA] DESC;

Even though you can use AREA in other columns.
 

Users who are viewing this thread

Back
Top Bottom