【发布时间】:2017-01-27 08:38:32
【问题描述】:
是否可以使用窗口函数进行累积乘法(如下查询)
select Id, Qty
into #temp
from(
select 1 Id, 5 Qty
union
select 2, 6
union
select 3, 3
)dvt
select
t1.Id
,exp(sum(log( t2.Qty))) CumulativeMultiply
from #temp t1
inner join #temp t2
on t2.Id <= t1.Id
group
by t1.Id
order
by t1.Id
喜欢:
select
t1.Id
,exp(sum(log( t2.Qty))) over (partition by t1.Id order by t1.Id rows between unbounded preceding and current row ) CumulativeMultiply
from #temp t1
inner join #temp t2
on t2.Id <= t1.Id
但得到错误:
函数 'exp' 不是有效的窗口函数,不能与 OVER 子句一起使用
更新: 我真正想要的结果:
Id CumulativeMultiply
----------- ----------------------
1 5
2 30
3 90
【问题讨论】:
-
你刚刚弄错了括号 - 你希望
OVER应用于SUM,所以它需要 inside @987654326 的括号@. -
exp()不是聚合,所以不能作为窗口函数使用
标签: sql sql-server tsql window-functions exp