Use GROUP BY UserId
, so rows with the same user ID are grouped. The remaining columns of the SELECT
list should use aggregate functions. You want SUM()
for StateDuration
and probably MIN()
or MAX()
(or both!) For StatusDateTime
.
You can also use aliases to improve readability:
SELECT MIN(aal.StatusDateTime) AS MinStatusDateTime, MAX(aal.StatusDateTime) AS MaxStatusDateTime, aal.UserId, SUM(aal.StateDuration / 60) AS Minutes, 'Shadow Trainer' AS StatusKey FROM UserWorkgroups AS uw INNER JOIN AgentActivityLog AS aal ON uw.UserId = aal.UserId WHERE aal.StatusDateTime BETWEEN '2013-08-01' AND '2013-08-02' AND aal.StatusKey IN ('Shadow Trainer') AND uw.WorkGroup IN ('LV_GR_9','LV_CCS_GENERAL','LV_PBX_INTERNAL') GROUP BY aal.UserId ORDER BY MinStatusDateTime, UserId, StatusKey, Minutes ;
source share