jrrodasm
jrrodasm

Reputation: 111

Calculated column based on another calculated field BigQuery

Hello I'm trying to use an already calculated value in a select to calculate another value, for example:

SELECT 1+1 AS two, ***two* + 2** AS four FROM table

is this possible in BigQuery? I'm sure it's on SAS with the keyword CALCULATED as follows:

SELECT 1+1 as two, ***CALCULATED two* + 2** AS four FROM table

Thank you,

Upvotes: 2

Views: 2243

Answers (2)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 173171

Below is example of how I personally approach such scenario (quite frequently btw)

SELECT two, two + 2 AS four 
FROM table, UNNEST([STRUCT(1+1 as two)])     

Upvotes: 5

Damião Martins
Damião Martins

Reputation: 1849

You cannot use an alias in same select. An option could be use a subselect:

WITH my_table AS (
  SELECT 1+1 AS two FROM table
)
SELECT two + 2 AS four FROM my_table

Upvotes: 2

Related Questions