With the following query I have created a very simple result with two rows and two columns:
SELECT
a.type,
AVG(DATEDIFF(a.completed, b.created)) AS avg_time_delta
FROM
a
INNER JOIN a ON b.record_id = content.id
INNER JOIN b ON c.client_id = clients.id
WHERE
record_type = "content"
AND
c.id = "999"
AND a.completed IS NOT NULL
AND b.type = "article"
AND b.spiked = 0
AND (a.type = "TCE sign-off" OR a.type = "Final sign-off")
GROUP BY a.type
| type | avg_time_delta |
|---------------------|------------------|
| Final sign-off | 20 |
| TCE sign-off | 10 |
I am trying to transpose this result so that the columns are ["Final sign-off", "TCE sign-off"] and there is a single row of [20, 10]. I am really struggling to do this in MySql without errors. Here is my attempt, any pointers would be appreciated.
SELECT *
FROM (
SELECT
a.type,
AVG(DATEDIFF(a.completed, b.created)) AS avg_time_delta
FROM
a
INNER JOIN a ON b.record_id = content.id
INNER JOIN b ON c.client_id = clients.id
WHERE
record_type = "content"
AND
c.id = "999"
AND a.completed IS NOT NULL
AND b.type = "article"
AND b.spiked = 0
AND (a.type = "TCE sign-off" OR a.type = "Final sign-off")
GROUP BY a.type
) AS source_table
PIVOT (
avg_time_delta
FOR type in ("Final sign-off", "TCE sign-off")
) AS pivot_table