Si está utilizando MySQL 8+, podemos manejar esto usando ROW_NUMBER
:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Date ORDER BY Time) rn
FROM attendancedata
WHERE EnrolledID = 23
)
SELECT
Date,
MAX(CASE WHEN rn = 1 THEN Time END) AS PO1,
MAX(CASE WHEN rn = 2 THEN Time END) AS PO2,
MAX(CASE WHEN rn = 3 THEN Time END) AS PO3,
MAX(CASE WHEN rn = 4 THEN Time END) AS PO4,
MAX(CASE WHEN rn = 5 THEN Time END) AS PO5,
MAX(CASE WHEN rn = 6 THEN Time END) AS PO6,
MAX(CASE WHEN rn = 7 THEN Time END) AS PO7
FROM cte
GROUP BY
Date
ORDER BY
Date;
Pero tenga en cuenta que, en general, es deseable almacenar la fecha y la hora en un solo datetime
campo con MySQL. En este caso particular, funcionó bien, pero puede que no siempre sea así.