Statistic analysis: SQL-monster for pending-reminder-overtime

Hi, I created an SQL-monster which shows all overtimes for pending tickets within an selected month. Check it out... Bye, Alex /* shows all overtime of finished pending-reminder states which pending-time-set was done within the selected YEAR/MONTH */ SELECT (SELECT tn FROM ticket WHERE id = th2.ticket_id) AS Ticket, th1.time1 AS timefrom, th2.create_time AS timeto, /* time when pending stops, perhaps overtime */ th1.timepending AS timeuntil, (UNIX_TIMESTAMP(th2.create_time)-UNIX_TIMESTAMP(th1.timepending))/3600 AS overtime_24h, (SELECT SUBSTRING_INDEX(TRIM(BOTH '%%' FROM name), '%%', 1) FROM ticket_history WHERE id = idstate) AS state FROM ( /* search for set pending-time */ SELECT TIMESTAMP(TRIM(BOTH '%%' FROM th.name)) AS timepending, /* pending-time */ /*SELECT MAX(id) FROM ticket_history WHERE ticket_id = th.ticket_id AND history_type_id IN (1, 26, 27) AND id < th.id) AS id1,*/ (SELECT MIN(id) FROM ticket_history WHERE ticket_id = th.ticket_id AND history_type_id IN (26, 27) AND id > th.id) AS id2, /* history-entry with next state-update or set pending-time */ (SELECT MIN(id) FROM ticket_history WHERE ticket_id = th.ticket_id AND history_type_id IN (27) AND id > th.id) AS idstate, /* history-entry with next state-update */ th.create_time AS time1 /* time when pending starts */ FROM ticket_history th WHERE th.history_type_id = 26 /* set pending-time history-entry */ AND YEAR(th.create_time) = 2006 /* ===>>> YEAR */ AND MONTH(th.create_time) = 04 /* ===>>> MONTH */ ) AS th1 INNER JOIN ticket_history th2 ON th1.id2 = th2.id /* union with next state-update or pending-time-update */ WHERE th1.idstate IS NOT NULL AND /* state must be finished */ (SELECT SUBSTRING_INDEX(TRIM(BOTH '%%' FROM name), '%%', 1) FROM ticket_history WHERE id = idstate) IN (SELECT name FROM ticket_state WHERE type_id = 4) AND /* only pending-reminder states */ th1.time1 < th1.timepending /* pending-time must be greater than its set pending time - should always be true */
participants (1)
-
Alexander Scholler