Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference a projected/selected column?

Tags:

sql

sql-server

I project a column in my select statement. ("project" in the relational algebra sense.) With the goal of reducing code duplication, is there a way to reference that projected column in my where clause? Or is there a better way to do that?

Example:

select
  (A.Column + A.Column2) * 8 'Column'
from A
where
  (A.Column + A.Column2) * 8 < 1000

Basically, what I'm asking, if we think of columns as being "namespaced" by table (where A is a namespace and A.Column is the Column in the A namespace), is: is there a way to refer to the namespace for the ephemeral table we're currently selecting in the where clause of that table itself?

like image 898
Tyler Avatar asked Jun 23 '26 02:06

Tyler


1 Answers

Another way is to use cte, common table expression.

with cte as(
  select (A.Column + A.Column2) * 8 as [Column] from A
)
Select * from cte
Where [Column] < 1000
like image 86
Ahmed Saeed Avatar answered Jun 24 '26 19:06

Ahmed Saeed