是否可以使用窗口函数进行累加乘法(低于查询)
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
但是得到错误:
功能&#39; exp&#39;不是有效的窗口函数,不能与OVER子句
一起使用
更新 实际上我想要的结果:
Id CumulativeMultiply
----------- ----------------------
1 5
2 30
3 90
答案 0 :(得分:1)
Sum Over(Order by)
无需自联接以查找以前的记录并将其相乘
select
Id
,exp(sum(log( Qty))
over (order by Id )) CumulativeMultiply from #temp
答案 1 :(得分:0)
只有聚合函数才是有效的窗口函数。
我没有测试代码,但你需要以某种方式分离2:
SELECT Id, exp(cm) CumulativeMultiply
FROM (
select
Id
,sum(log(Qty)) over (partition by Id order by Id rows between unbounded preceding and current row ) cm
from #temp
) d