You can try
select *, datediff(days,lag(cast(end_date as date),1)
over(partition by client,unique_id order by end_date),
cast(start_date as date))
as day_gap
from tablename
The datediff function will vary depending on the dialect of SQL you're using. You can check the syntax online for that particular dialect
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.