1

是否可以使用窗口函数进行累积乘法(如下查询)

    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
4

2 回答 2

1

无需自我加入Sum Over(Order by)即可找到以前的记录并将其相乘

 select  
        Id
        ,exp(sum(log( Qty))
        over (order by Id )) CumulativeMultiply from #temp 
于 2017-01-27T08:51:27.890 回答
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
于 2017-01-27T08:44:26.197 回答