连续两次负余额之差

问题描述 投票:0回答:0

账户连续负余额多少天。 我不会在查询中提取内部状态,它存在于数据中,只要余额为负,它就会显示“N”

数据看起来像

我得到的查询输出

with cte as (
select *,row_number() over partition by account order by load date) as rn
from table
where balance <0 and account='ABCD'
datediff(c1.load date,c2.load_date) as days_between
from cte c1
join cte c2
on c1.account=c2.account and c1.rn=c2.rn-1

我想要达到的输出

余额为负时最大和最小连续加载日期之间的天数差

sql join window-functions
© www.soinside.com 2019 - 2024. All rights reserved.