账户连续负余额多少天。 我不会在查询中提取内部状态,它存在于数据中,只要余额为负,它就会显示“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
余额为负时最大和最小连续加载日期之间的天数差