3 回答
TA贡献1818条经验 获得超3个赞
您还可以使用LAG分析功能来获得所需结果,如下所示:
假设下面是您的输入表:
id account_number account_date
1 1001 9/10/2011
2 2001 9/1/2011
3 2001 9/3/2011
4 1001 9/12/2011
5 3001 9/18/2011
6 1001 9/20/2011
select id,account_number,account_date,
datediff(day,lag(account_date,1) over (partition by account_number order by account_date asc),account_date)
as day_diffrence
from yourtable;
这是您的输出:
id account_number account_date day_diffrence
1 1001 9/10/2011 NULL
4 1001 9/12/2011 2
6 1001 9/20/2011 8
2 2001 9/1/2011 NULL
3 2001 9/3/2011 2
5 3001 9/18/2011 NULL
- 3 回答
- 0 关注
- 622 浏览
添加回答
举报