我正在做与这里相同的事情 Date Difference between consecutive rows
但是,我希望结果显示给我每个帐户编号天平均差异,而天上每个差异不一行。
这样的事情。
当我添加avg(datediff(d1,d2))时,出现以下错误:
[代码:-112,SQL状态:42607]列函数“ AVG”的操作数包括列函数,标量全选或子查询。SQLCODE = -112,SQLSTATE = 42607,DRIVER = 4.22。 29
PS:
答案 0 :(得分:0)
在这种情况下,使用lead
OLAP函数很方便:
with tab (ID, Account, Date) as (values
(1, 1001, date('2011-09-10'))
, (2, 2001, date('2011-09-01'))
, (3, 2001, date('2011-09-03'))
, (4, 1001, date('2011-09-12'))
, (5, 3001, date('2011-09-18'))
, (6, 1001, date('2011-09-20'))
)
select account, avg(days(date_next) - days(date)) avg_diff
from (
select id, account, date, lead(date) over (partition by account order by date) date_next
from tab
)
group by account;