创建一个引用另一个计算列的计算列。

huangapple go评论53阅读模式
英文:

Creating a calculated column that references another calculated column

问题

我正在使用SQL在Databricks中工作,正在创建许多带有长方程式的列,我想要做的是将计算的列用作其他计算列中的引用/变量。

我想要实现的目标

选择
Premium as Premium
Premium + B as Premium1,
Premium1 + C as Premium2,(其中Premium1 = Premium + B,我们已创建)
Premium2 + D as Premium3
from TABLENAME

是否有办法实现这一点?谢谢!

英文:

I'm working in Databricks in SQL and im creating a bunch of columns with long equations, what I would like to do is use the calculated column as a reference/variable in other calculated columns.

EXAMPLE of some code I have:

Select
Premium as Premium,
Premium + B as Premium1,
Premium + B + C as Premium2,
Premium + B + C + D as Premium3
from TABLENAME

What I want to achieve:

Select
Premium as Premium
Premium + B as Premium1,
Premium1 + C as Premium2, (where Premium1 = Premium + B which we created)
Premium2 + D as Premium3
from TABLENAME

Is there a way to do this? Thanks?

答案1

得分: 0

你可以将其作为CTE链来执行,如下所示:

with 
   p1 as (
       select premium, premium + B as premium1
       from tablename
   ),
   p2 as (
       select premium, premium1, premium1 + C as premium2
       from p1
   ),
   p3 as (
       select premium, premium1, premium2, premium2 + D as premium3
       from p2
   )
select
    *
from 
    p3
英文:

You can do it as a chain of CTEs like this:

with 
   p1 as (
       select premium, premium + B as premium1
       from tablename
   ),
   p2 as (
       select premium, premium1, premium1 + C as premium2
       from p1
   ),
   p3 as (
       select premium, premium1, premium2, premium2 + D as premium3
       from p2
   )
select
    *
from 
    p3

huangapple
  • 本文由 发表于 2023年2月16日 02:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定