如何重用计算列避免重复sql语句? [英] How to reuse calculated columns avoiding duplicating the sql statement?

查看:30
本文介绍了如何重用计算列避免重复sql语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多计算过的列,它们不断重复,一个在其他列中,包括嵌套的 case 语句.

I have a lots of calculated columns and they keep repeting themselves, one inside of the others, including nested cases statements.

我正在寻找一种方法来做某事的真正简化版本.

There is a really simplified version of something that i've searching a way to do.

SELECT 
    (1+2) AS A, 
    A + 3 AS B, 
    B * 7 AS C 
FROM MYTABLE

提前致谢.

推荐答案

你可以尝试这样的事情.

You could try something like this.

SELECT 
    A.Val AS A, 
    B.Val AS B, 
    C.Val AS C 
FROM MYTABLE
  cross apply(select 1 + 2) as A(Val)
  cross apply(select A.Val + 3) as B(Val)
  cross apply(select B.Val * 7) as C(Val)

这篇关于如何重用计算列避免重复sql语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆