SQL 获取特定模式的命令
SQL command for getting a specific pattern
我有一个 table 样本,列 'observations':
请帮助 SQL 命令获得以下 'cumulative multiplication' 输出:
2
6
30
300
一种方法是递归 CTE:
with tt as (
select t.*, row_number() over (order by obs) as seqnum
from t
),
cte as (
select obs as prod, seqnum
from tt
where seqnum = 1
union all
select cte.prod * tt.obs, tt.seqnum
from cte join
tt
on tt.seqnum = cte.seqnum + 1
)
select *
from cte;
另一个使用算术实现一个“乘积”window函数:
select t.*,
exp(sum(log(obs)) over (order by obs))
from t;
Here 是一个 db<>fiddle.
我有一个 table 样本,列 'observations':
请帮助 SQL 命令获得以下 'cumulative multiplication' 输出:
2
6
30
300
一种方法是递归 CTE:
with tt as (
select t.*, row_number() over (order by obs) as seqnum
from t
),
cte as (
select obs as prod, seqnum
from tt
where seqnum = 1
union all
select cte.prod * tt.obs, tt.seqnum
from cte join
tt
on tt.seqnum = cte.seqnum + 1
)
select *
from cte;
另一个使用算术实现一个“乘积”window函数:
select t.*,
exp(sum(log(obs)) over (order by obs))
from t;
Here 是一个 db<>fiddle.