How to reuse calculated columns avoiding duplicating the sql statement?

后端 未结 6 1360
忘掉有多难
忘掉有多难 2020-12-09 07:51

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 ver

6条回答
  •  被撕碎了的回忆
    2020-12-09 08:51

    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)
    

提交回复
热议问题