Hi Jason-299789,
Sorry for the late reply. I had to do another project in between and this had to wait.
But, thank you very much for your answer.
It works the way I want it with your query adjusted in this way:
WITH UserList_CTE
AS(
SELECT ROW_NUMBER() OVER (PARTITION BY AU.Useraccount_ID ORDER BY US.Useraccount_Status_DateTime) Rn
, US.Useraccount_Status_DateTime
, US.Useraccount_Status_Type_ID
, AU.Useraccount_ID
FROM Application_Useraccounts_Status AS US
INNER JOIN Application_Useraccounts AS AU
ON US.Useraccount_ID = AU.Useraccount_ID
INNER JOIN Application_Useraccounts_Status_Types AS ST
ON US.Useraccount_Status_Type_ID = ST.Useraccount_Status_Type_ID
WHERE (US.Useraccount_Status_DateTime BETWEEN @beginDatum AND @eindDatum)
)
Select
AU.Useraccount_First_Name
, AU.Useraccount_Last_Name
, currul.Useraccount_Status_Type_ID
, currul.Useraccount_Status_DateTime
, Convert(time, futureul.Useraccount_Status_DateTime-currul.Useraccount_Status_DateTime) Tijdsduur
From
Application_Useraccounts AU
INNER JOIN UserList_CTE currul
ON AU.Useraccount_ID=currul.Useraccount_ID
LEFT JOIN UserList_CTE futureul
on currul.Useraccount_ID=futureul.Useraccount_ID
AND currul.Rn+1=futureul.rn
Calculating the totals I will have to do in C# code (requirement of my boss).
Now I can continue with this project.
Greetz,
Geert