英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论