mysql - Query to convert columns into rows

Asked

Viewed 592 times

1

I have my appointment:

SELECT inscricao, assiduos_a1, assiduos_a2, max(data_inclusao)
  FROM cli_agendados_assiduos
 WHERE INSCRICAO = 10357410;

Returning:

inscricao   assiduos_a1 assiduos_a2 max(data_inclusao)
10357410    337         508         08/01/2015 14:54:48

But I need the results in lines:

Label       Valor   Inscrição   max(data_inclusao)
assiduos_a1 337     10357410    08/01/2015 14:54
assiduos_a2 508     10357410    08/01/2015 14:54

only this way my chart (google pie Chart) will work correctly

1 answer

1

I could only think of something like:

SELECT *
FROM (
    SELECT 'assiduos_a1' AS 'Label', assiduos_a1 AS 'Valor', inscricao, max(data_inclusao)
    FROM cli_agendados_assiduos
    UNION
    SELECT 'assiduos_a2' AS 'Label', assiduos_a2 AS 'Valor', inscricao, max(data_inclusao)
    FROM cli_agendados_assiduos
) a
WHERE inscricao = 10357410;

But I think you better rethink the structure of your table. Search for "Pivot Tables".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.