Assuming that the start and end dates will always be the highest values then you need to drop some of the columns from the GROUP BY
(having all the columns in the GROUP BY
is kinda like using DISTINCT
) and use an aggregate function on the other column:
SELECT UserId,
MAX(StartDate) AS StartDate,
MAX(EndDate) AS EndDate
FROM usersworktime
GROUP BY UserId;
Otherwise, if that isn't the case, you can use a CTE and ROW_NUMBER
:
WITH CTE AS(
SELECT UserID,
StartDate,
EndDate,
ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY UsersWordTimeID DESC) AS RN
FROM usersworktime)
SELECT UserID,
StartDate,
EndDate
FROM CTE
WHERE RN = 1;