Here is the suggestion that uses the classic pivot to generate the 4 columns.
-- código #1 v3
with R070ACCseq as (
SELECT numcad, datacc, horacc,
seq= row_number() over (partition by numcad, cast(datacc as date)
order by (SELECT 0))
from R070ACC
where datacc between '2018-04-16' and '2018-05-15'
and numcad = '2676'
and oriacc = 'E'
)
SELECT numcad, datacc,
max(case when seq = 1 then horacc end) as REG1,
max(case when seq = 2 then horacc end) as REG2,
max(case when seq = 3 then horacc end) as REG3,
max(case when seq = 4 then horacc end) as REG4
from R070ACCseq
group by numcad, datacc;
A table is a set of data without order. If it is necessary that for the same date the columns REG1 to REG4 list the values in the order in which they appear in the example, then it is necessary that there is a column indicating in which order the rows should be manipulated.
(...) has the shape of [hh:mm]?
-- código #1 v4
set dateformat ydm;
with R070ACCseq as (
SELECT numcad, cast(datacc as date) as datacc,
convert(char(5), dateadd(minute, horacc, 0), 108) as horacc,
seq= row_number() over (partition by numcad, cast(datacc as date)
order by horacc)
from R070ACC
where datacc between '2018-16-05' and '2018-15-06'
and numcad = '2676'
)
SELECT numcad, datacc,
max(case when seq = 1 then horacc end) as REG1,
max(case when seq = 2 then horacc end) as REG2,
max(case when seq = 3 then horacc end) as REG3,
max(case when seq = 4 then horacc end) as REG4
from R070ACCseq
group by numcad, datacc;
Do you want to do this to display in four columns? Or organize the backend with 4 columns?
– Dudut
In addition to the DATACC column, there is another column that informs the sequence in which the lines should be read, for the same date?
– José Diz