【发布时间】:2020-07-21 16:49:49
【问题描述】:
我有一个如下所示的表格:
+---------+-------------------+-------+
| merchant|time |amount |
+---------+-------------------+-------+
| 1 |2020-04-01 10:15:01| 1234|
| 2 |2020-04-01 10:15:02| 50|
| 1 |2020-04-01 10:15:15| 820|
| 1 |2020-04-01 10:15:20| 29|
| 2 |2020-04-01 10:15:21| 260|
+---------+-------------------+-------+
我希望获得每个商家每分钟的平均交易次数。
我可以做类似的事情
select
merchant,
avg(transactions_per_minute)
from (
select
merchant,
date_trunc('minute', time) as time,
count(*) as transactions_per_minute
from transactions
group by 1, 2
)
但是有没有办法用窗口函数来做到这一点?我在想象类似的东西
select
merchant,
avg(count(*) over (partition by date_trunc('minute', time)) as transactions_per_minute
from transactions
group by 1
但这会引发错误Cannot nest window functions inside aggregation 'avg'
【问题讨论】:
标签: sql window-functions presto