Ludovic Migneault Ludovic Migneault - 23 days ago 9
SQL Question

Return a crosstab to its original table format

I'm often recieving datas from an other departement in a "cross-tab" way.

color_|_person1_|_person2_|_person3___|
red | yes | yes | no
Blue | no | no | yes
Green | no | yes | no


(this is just a mere example)

And actually the rows (Person[1-2-3]) are an ID in an other table.

So it should instead look like that :

IDPerson__|_Color__|_Activated_|
person1 | red | yes
person1 | Blue | yes
person1 | Green | no
etc...


These datas are on Excel and Access 2003,
is there any way to "un-cross" the table automatically?

Making a crosstab from a regular table is simple enough, however I haven't found any ways to do the oposite yet. :/

Thanks for helping! ^^

Answer

MS Access does not have an UNPIVOT function which will convert your columns of data into rows but you can use a UNION ALL query:

SELECT 'person1' as IDPerson, color, person1 as activated
FROM yourtable
UNION ALL
SELECT 'person2' as IDPerson, color, person2 as activated
FROM yourtable
UNION ALL
SELECT 'person3' as IDPerson, color, person3 as activated
FROM yourtable

See Demo (SQL Server Demo) to see working version.