0
Vyou can use multiple sub-selects for this, as I understand it, the order of the columns is given by the date column, and will always be 4 columns. So:
select distinct
h.ticket,
(select x.fila from historico x where x.ticket = h.ticket order by data limit 1) as fila_1,
(select x.data from historico x where x.ticket = h.ticket order by data limit 1) as data_fila_1,
(select x.fila from historico x where x.ticket = h.ticket order by data limit 1 offset 1) as fila_2,
(select x.data from historico x where x.ticket = h.ticket order by data limit 1 offset 1) as data_fila_2,
(select x.fila from historico x where x.ticket = h.ticket order by data limit 1 offset 2) as fila_3,
(select x.data from historico x where x.ticket = h.ticket order by data limit 1 offset 2) as data_fila_3,
(select x.fila from historico x where x.ticket = h.ticket order by data limit 1 offset 3) as fila_4,
(select x.data from historico x where x.ticket = h.ticket order by data limit 1 offset 3) as data_fila_4
from historico h
Upshot:
ticket fila_1 data_fila_1 fila_2 data_fila_2 fila_3 data_fila_3 fila_4 data_fila_4
61100 37 2018-06-01T10:00:00Z 12 2018-06-01T18:00:00Z 20 2018-06-04T09:00:00Z 35 2018-06-05T17:00:00Z
71100 37 2018-06-02T10:00:00Z 12 2018-06-02T18:00:00Z 20 2018-06-05T09:00:00Z 35 2018-06-06T17:00:00Z
I put in the Sqlfiddle
Note: Insert another example ticket.
fila_1
,fila_2
.... only 4 rows or is variable as well ?– Rovann Linhalis
Only 4 rows, but there may be cases where there are only 3 rows. The idea is to make a calculation of how long each ticket was in each queue, I already created a Function to make this calculation, but the parameters to be passed to Function is the ticket number and the queue number, hence the return of Function will be the time between a queue and another, for example: 02:00:00 so I need the result to be all in one row, same in table 2.
– Jorgito da Silva Paiva
The order of records you mean? There is the id field that was omitted in table 1, just consider that each row has a unique id, tried with subselect but unsuccessfully.
– Jorgito da Silva Paiva
@Rovann Linhalis numbers 37, 12, 20 and 35 are the Queue Ids that this 61100 ticket passed, this table is a history in a ticket system.
– Jorgito da Silva Paiva
A new ticket will always have the first entry in row 37 and always with history_id = 1. So when you change queue history_id will always be = 16 and the Queue id may vary, each queue has its unique id.
– Jorgito da Silva Paiva