Can I use aliases within a select statement?

Loofer

I would like to write a statement that looks like this

select 1 as one, 2 as two , one + two as three

However SQL Server cannot work out what one + two is.

I can re-write in this form

SELECT x.one
      ,x.two
      ,x.one + x.two AS three
FROM (
    SELECT 1 AS one
          ,2 AS two
    ) x

Which gives me the output I expect. It is just a bit messy (and more so in a non-contrived example) I am also not sure what this sort of thing does to the internals of SQL Server and what implications there are to speed of execution.

Is there a better way of doing this?

Tim Schmelter

You cannot refer to an alias in the same SELECT, you need to define it in a sub-query(like you did) or in a Common-table-expression(CTE):

WITH CTE AS
(
    SELECT 1 as one, 2 as two
)
SELECT one, two, one + two AS three FROM CTE

Or with this syntax:

WITH CTE(one, two) AS
(
    SELECT 1, 2
)
SELECT one, two, one + two as three from CTE

The same rule applies to the WHERE: Reference alias (calculated in SELECT) in WHERE clause

But normally it doesn't hurt if you use the same expression multiple time, the sql server optimizer will evaluate it only once. So you could do:

SELECT 1 as one, 2 as two , 1 + 2 as three

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can i use Scanner within a catch statement

Can I use an if statement within a for loop

Why can't I use column aliases in the next SELECT expression?

How can I use IF and CONCAT within SELECT

How can I create a dynamic Select statement within a SQL Select statement?

Can I use filter on a formula within a postgres statement?

Can I use another cfswitch statement within the same cfswitch case

SwiftUI - Why can't I use an Alert within an if statement

How can I use an if/else-statement within predicates?

Can I use OPTION (MAXDOP 1) for a query within an IF statement?

Can I use YEAR(CURDATE()) within a like statement?

Can I use an if statement within a React useEffect hook like this?

Use a select within a select statement in Oracle

Can I use which command on aliases?

How can I use SSH aliases with rsync?

How to use a variable defined outside of a select statement within a select statement

How can I use a postgres variable in my SELECT statement?

Can I use the result of a select statement from mysql to compare it with a variable?

How can I use a variable inside a SELECT statement?

Can I use a select statement for inserting values with an object type?

Use COALESCE within SUM in MySQL SELECT statement

How to use if statement within Select case argument

ROW_NUMBER() ORDER BY, can't use column within same SELECT statement

Can I use an IF statement within my SQL Server query to use a query on multiple SQL versions?

select statement w/ TWO AS aliases?

How can I replace within a SELECT the statement from one table to ther other one?

PostgreSQL Use a value from a select statement as a variable within that same statement

Can we use ternary operator within an if statement?

select statement within select