I tried this solution :
SELECT Id
,[move_in_date]
,[move_out_date]
,LAG(move_out_date) OVER(Partition By Id ORDER BY move_in_date) as Lag_out
FROM MyTable
ORDER BY isnull(move_out_date,LAG(move_out_date) OVER(Partition By Id ORDER BY move_in_date)) DESC
I don't know if this will work with every scenario i mentioned in my first post
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…