Answer the question
In order to leave comments, you need to log in
Is it possible in SQL to create aliases in a where clause and use it in a select?
For example, there is a Prices table with prices. all ids in the table are sorted in ascending order.
You need to get price ranges where the maximum price is greater than a certain number.
Here is an example request;
SELECT p1.id as id1, p2.id as id2, (SELECT MAX(price) FROM Prices p3 WHERE p3.id >= id1 AND p3.id <= id2 )
FROM Prices as p1, Prices as p2
WHERE (SELECT MAX(price) FROM Prices p3 WHERE p3.id >= id1 AND p3.id <= id2 ) > 150
(SELECT MAX(price) FROM Prices p3 WHERE p3.id >= id1 AND p3.id <= id2 )
SELECT p1.id as id1, p2.id as id2, subq
FROM Prices as p1, Prices as p2
WHERE (SELECT MAX(price) FROM Prices p3 WHERE p3.id >= id1 AND p3.id <= id2 ) as subq> 150
Answer the question
In order to leave comments, you need to log in
You need to get price ranges where the maximum price is greater than a certain number.
In the ANSI SQL standard and ISO SQL standard, the value that is associated with a column alias does not need to be available until the ORDER BY clause is executed.
that's the standard.
but for those who wish, there are examples of circumventing restrictions through cte. although from the point of view of query optimality, this does not provide any advantages.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question