Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I currently have a table of data that shows different steps in a process, with a date/time each step was carried out.

enter image description here

What I'm looking to do is add a column that calculates the time in minutes between each step, however it has to relate to the claimID, so in the image shown I would be looking for difference between each step for the top 4 results (as they share the same claimID), then the following 6 results, etc.

Can anyone help? I'm using SQL Server

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Depending on what version of SQL Server you are using you can either use a self join or the lag window function (this should work in SQL Server 2012+):

select 
    claimid
    , statusid
    , statussetdate
    , coalesce(datediff(minute, 
               lag(statussetdate) over (partition by claimid order by statussetdate), 
               statussetdate
              ),0) as diff_in_minutes
from 
    your_table
order by 
    ClaimID
    , StatusSetDate;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...