简体   繁体   中英

Subtract start date from the end date of the row before - SQL

I'm not able to subtract from the start date the end date of the row above (like in the image) to create a column where I can see if there has been gaps of time between rows.

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM