如何将算术和与 SQL 中的解析函数相结合以获得 运行

How to combine an arithmetic sum with an analytic function in SQL for a running

我正在尝试根据正常 SQL 中 table 的两次计数结果(如下面的 SQL 摘录所示)计算 运行 % ] 看起来像这样:

我的想法是这样的:

    SELECT week(beginning),
(select count(enquired_at) from TABLE) / (select count(*) from TABLE) * 100 OVER (partition by beginning order by beginning desc)

我想要一个如下所示的结果集:

一个人如何进行这种算术计算,使其从第 1 周开始累积计算?

谢谢!

根据您的查询,您似乎想要 运行 计数 enquired_at 不为空的次数。如果是:

select t.*,
       ( count(enquired_at) over (order by beginning) * 1.0 /
         count(*) over (order by beginning)
       ) as ratio
from t;

或更简单地说:

select t.*,
       avg(case when enquired_at is not null then 1.0 else 0 end) over (order by beginning) as ratio
from t

编辑:

您可以将其放入聚合查询中:

select week(beginning), 
       ( sum(count(enquired_at)) over (order by min(beginning)) * 1.0 /
         sum(count(*)) over (order by min(beginning))
       ) as ratio
from t
group by week(beginning);