SQL + K1000: time spent on hotline per department, for every technicians
Hello,
In order to finish my PowerBI dashboard, i'm currently trying to calculate the time spent on the tickets resolution for each department, per year and for every technician
Here is what I end up with (where DIFF is the sum of hours spent)
SELECT count(HD_TICKET.ID) as total,
HD_TICKET.CUSTOM_FIELD_VALUE0 as department,
CASE
WHEN HOUR(HD_TICKET.CREATED) between 0 and 7 THEN @startdate := concat(date(HD_TICKET.CREATED),' 08:00:00')
WHEN HOUR(HD_TICKET.CREATED) between 18 and 24 AND DAYofweek(HD_TICKET.CREATED) != DAYofweek(HD_TICKET.TIME_CLOSED) THEN @startdate := concat(date(date_ADD(HD_TICKET.CREATED, INTERVAL 1 day)),' 08:00:00')
WHEN HOUR(HD_TICKET.CREATED) between 18 and 24 AND DAYofweek(HD_TICKET.CREATED) = DAYofweek(HD_TICKET.TIME_CLOSED) THEN @startdate := concat(date(HD_TICKET.CREATED),' 18:00:00')
WHEN HOUR(HD_TICKET.CREATED) between 12 and 13 THEN @startdate := concat(date(HD_TICKET.CREATED),' 14:00:00')
else @startdate := HD_TICKET.CREATED
END,
CASE
WHEN HOUR(HD_TICKET.TIME_CLOSED) between 0 and 7 THEN @closedate := concat(date(HD_TICKET.TIME_CLOSED),' 08:00:00')
WHEN HOUR(HD_TICKET.TIME_CLOSED) between 18 and 24 THEN @closedate := concat(date(HD_TICKET.TIME_CLOSED),' 18:00:00')
WHEN HOUR(HD_TICKET.TIME_CLOSED) between 12 and 13 THEN @closedate := concat(date(HD_TICKET.TIME_CLOSED),' 12:00:00')
else @closedate := HD_TICKET.TIME_CLOSED
END,
CASE
WHEN HOUR(HD_TICKET.TIME_OPENED) between 0 and 7 THEN @opendate := concat(date(HD_TICKET.TIME_OPENED),' 08:00:00')
WHEN HOUR(HD_TICKET.TIME_OPENED) between 18 and 24 AND DAYofweek(HD_TICKET.TIME_OPENED) != DAYofweek(HD_TICKET.TIME_CLOSED) THEN @opendate := concat(date(date_ADD(HD_TICKET.TIME_OPENED, INTERVAL 1 day)),' 08:00:00')
WHEN HOUR(HD_TICKET.TIME_OPENED) between 18 and 24 AND DAYofweek(HD_TICKET.TIME_OPENED) = DAYofweek(HD_TICKET.TIME_CLOSED) THEN @opendate := concat(date(HD_TICKET.TIME_OPENED),' 18:00:00')
WHEN HOUR(HD_TICKET.TIME_OPENED) between 12 and 13 THEN @opendate := concat(date(HD_TICKET.TIME_OPENED),' 14:00:00')
else @opendate := HD_TICKET.TIME_OPENED
END,
@startdate as startdate,
@closedate as closedate,
@opendate as opendate,
CASE
WHEN (DAYofweek(@startdate) = DAYofweek(@closedate) AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@closedate) BETWEEN 8 and 12) THEN timediff(@closedate,@startdate)
WHEN (DAYofweek(@startdate) = DAYofweek(@closedate) AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@closedate) BETWEEN 14 and 18) THEN timediff(@closedate,@startdate)
WHEN (DAYofweek(@startdate) = DAYofweek(@closedate) AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@closedate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@closedate,@startdate),'0 2:00:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@closedate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@closedate,@startdate),'0 16:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@closedate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@closedate,@startdate),'0 14:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@closedate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@closedate,@startdate),'0 16:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@closedate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@closedate,@startdate),'0 18:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@closedate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@closedate,@startdate),'0 40:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@closedate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@closedate,@startdate),'0 38:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@closedate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@closedate,@startdate),'0 40:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@closedate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@closedate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@closedate,@startdate),'0 42:30:00.000000')
ELSE maketime(0,0,0)
END as DIFF,
CASE
WHEN (DAYofweek(@startdate) = DAYofweek(@opendate) AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@opendate) BETWEEN 8 and 12) THEN timediff(@opendate,@startdate)
WHEN (DAYofweek(@startdate) = DAYofweek(@opendate) AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@opendate) BETWEEN 14 and 18) THEN timediff(@opendate,@startdate)
WHEN (DAYofweek(@startdate) = DAYofweek(@opendate) AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@opendate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@opendate,@startdate),'0 2:00:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@opendate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@opendate,@startdate),'0 16:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@opendate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@opendate,@startdate),'0 14:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@opendate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@opendate,@startdate),'0 16:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) != 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@opendate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@opendate,@startdate),'0 18:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@opendate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@opendate,@startdate),'0 40:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@opendate) BETWEEN 8 and 12) THEN SUBTIME(timediff(@opendate,@startdate),'0 38:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 14 and 18 AND HOUR(@opendate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@opendate,@startdate),'0 40:30:00.000000')
WHEN (DAYofweek(@startdate) != DAYofweek(@opendate) AND DAYofweek(@startdate) = 7 AND HOUR(@startdate) BETWEEN 8 and 12 AND HOUR(@opendate) BETWEEN 14 and 18) THEN SUBTIME(timediff(@opendate,@startdate),'0 42:30:00.000000')
ELSE maketime(0,0,0)
END as OPENED
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
WHERE (HD_STATUS.NAME not like '%Server Status Report%')
AND ((HD_STATUS.STATE like '%closed%')
AND (HD_STATUS.NAME not like '%spam%')
AND (HD_TICKET.HD_QUEUE_ID = 3)
AND (HD_TICKET.TIME_CLOSED > utc_timestamp() - interval 365 day)
)
group by department
order by DIFF DESC
The output seems to be wrong. Has anyone done something similar in the past ? I'm stuck with this one..
Regards