SQL 2008 - 征集命名公式
SQL 2008 - Call for named formula
假设我有这个代码:
Select
'Position Date' = todaypositiondate
,'Realized' = round(sum(realizedccy*spotsek),0)
FROM T1
group by todaypositiondate
order by todaypositiondate desc
如果我想将 'Realized' 除以 100。
如何设置 'new' = 'Realized'/100
而不是 'new' = (round(sum(realizedccy*spotsek),0))/100
?
此致
不确定为什么要这样做,但这是使用 CTE
的一种方法。您也可以使用子查询,但是这两个步骤对于您想要进行的数学运算来说毫无意义,并且会降低性能。您不能像您尝试做的那样在同一语句中通过它的别名来引用列别名。这包括在 where
或 group by
或 order by
子句中引用它
;with cte as(
Select
todaypositiondate as
round(sum(realizedccy*spotsek),0) as Realized
from T1
group by todaypositiondate
order by todaypositiondate desc)
select
todaypositiondate,
Realized,
Realized / 100 as RealizedDivHundred
from cte
假设我有这个代码:
Select
'Position Date' = todaypositiondate
,'Realized' = round(sum(realizedccy*spotsek),0)
FROM T1
group by todaypositiondate
order by todaypositiondate desc
如果我想将 'Realized' 除以 100。
如何设置 'new' = 'Realized'/100
而不是 'new' = (round(sum(realizedccy*spotsek),0))/100
?
此致
不确定为什么要这样做,但这是使用 CTE
的一种方法。您也可以使用子查询,但是这两个步骤对于您想要进行的数学运算来说毫无意义,并且会降低性能。您不能像您尝试做的那样在同一语句中通过它的别名来引用列别名。这包括在 where
或 group by
或 order by
子句中引用它
;with cte as(
Select
todaypositiondate as
round(sum(realizedccy*spotsek),0) as Realized
from T1
group by todaypositiondate
order by todaypositiondate desc)
select
todaypositiondate,
Realized,
Realized / 100 as RealizedDivHundred
from cte