Jeśli używasz MySQL 8+, poradzimy sobie z tym za pomocą 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;
Pamiętaj jednak, że generalnie pożądane jest przechowywanie daty i czasu w jednym datetime
pole z MySQL. W tym konkretnym przypadku zadziałało to dobrze, ale nie zawsze tak jest.