Assist in Cleaning a MySQL Join Query to Find Changes Between Rows -
table size ~500,000 rows, 137mb, query time 1.591 sec
i junior programmer testing mettle on our mysql based otrs ticket database. i'm looking instances in ticket moves between queues. indicator move exists on same row new queue, , have setup query attempts return previous value of same column in same ticket id.
example of ticket in table:
id history_type_id ticket_id queue_id create_time 377 1 30 2 2012-09-14 11:06:02 378 21 30 2 2012-09-14 11:06:02 379 12 30 2 2012-09-14 11:06:02 380 4 30 2 2012-09-14 11:06:02 381 16 30 8 2012-09-14 11:08:29 383 16 30 2 2012-09-14 11:09:54 384 17 30 2 2012-09-14 11:11:28 *** stuff happens *** 396 27 30 2 2012-09-14 11:22:06 397 18 30 2 2012-09-14 11:22:06
the rows in question 380, 381, , 383. history_type_id value move 16, , row above 381 has previous queue_id of ticket. able build query checks previous id value before id value history_type_id of 16 , shares same ticket_id , return queue_id of row (or assume. 100% self taught point). can assume, id , create_time not limited per ticket, , cannot used reliably sort.
here query:
select oldq.id, oldq.queue_id oldone, oldq.create_time firstq, newq.id, newq.queue_id newone, newq.create_time lastq, oldq.ticket_id ticket_history oldq join ticket_history newq on oldq.id < newq.id , oldq.ticket_id = newq.ticket_id newq.history_type_id = '16' order newq.ticket_id, newq.id, oldq.id
this "works" in ugly , unacceptable way, looks every value of oldq.id per ticket_id rather 1 before newq.id, , hit roadblock. have usable data, far more need. example, here output above ticket:
id oldone firstq id newone lastq ticket_id 377 2 2012-09-14 11:06:02 381 8 2012-09-14 11:08:29 30 378 2 2012-09-14 11:06:02 381 8 2012-09-14 11:08:29 30 379 2 2012-09-14 11:06:02 381 8 2012-09-14 11:08:29 30 380 2 2012-09-14 11:06:02 381 8 2012-09-14 11:08:29 30 377 2 2012-09-14 11:06:02 383 2 2012-09-14 11:09:54 30 378 2 2012-09-14 11:06:02 383 2 2012-09-14 11:09:54 30 379 2 2012-09-14 11:06:02 383 2 2012-09-14 11:09:54 30 380 2 2012-09-14 11:06:02 383 2 2012-09-14 11:09:54 30 381 8 2012-09-14 11:08:29 383 2 2012-09-14 11:09:54 30
where i'm attempting drive return such this:
id oldone firstq id newone lastq ticket_id 380 2 2012-09-14 11:06:02 381 8 2012-09-14 11:08:29 30 381 8 2012-09-14 11:08:29 383 2 2012-09-14 11:09:54 30
been puzzling 1 hours today, pleased when query ran, if these numbers can come alive, ecstatic
sytharin, haven't been able test yet, solution believe work follows:
select oldq.id, oldq.queue_id oldone, oldq.create_time firstq, newq.id, newq.queue_id newone, newq.create_time lastq, oldq.ticket_id ticket_history oldq join ticket_history newq on newq.id - 1 = oldq.id , oldq.ticket_id = newq.ticket_id newq.history_type_id = '16' order newq.ticket_id, newq.id, oldq.id
upon modifying join statement @ oldq.id directly behind newq.id, should limit results single row directly preceding newq.id. again, untested, small change in query see if works.
Comments
Post a Comment