Question : Transposing Columns to Rows in Access using SQL

Hello Experts,

I have the following problem: I need to intigrate a "bad" table no realtions, into a "good" database (relational). The "bad" db has one table with the following columns
 
ID
Name
RepPool
Bank
ParmOne
ParmValueOne
ParmTwo
ParmValueTwo
ParmThree
ParmValueThree
ParmFour
ParmValueFour
ParmFive
ParmValueFive

I'm happy with the , ID, Name, RepPool and Bank columns,
but I need to run a SQL query to transpose the "Parm" and "ParmValue" columns into rows. So the rows

1|File1|testID|Bank1|Parm1|p1|Parm2|p2|Parm3|p3|Parm4|p4|Parm5|p5
2|File2|testID|Bank1|Parm1|p1|Parm2|p2|Parm3|p3|null|null|null|null
3|File3|testID|Bank1|Parm1|p1|null|null|null|null|null|null|null|null

are transofrmed to:

1|File1|testID|Bank1|Parm1|p1
1|File1|testID|Bank1|Parm2|p2
1|File1|testID|Bank1|Parm3|p3
1|File1|testID|Bank1|Parm4|p4
1|File1|testID|Bank1|Parm5|p5
1|File1|testID|Bank1|Parm1|p1
2|File2|testID|Bank1|Parm2|p2
2|File2|testID|Bank1|Parm3|p3
3|File3|testID|Bank1|Parm1|p1

All I need is a query that does this transformation for me, so that external tools can treat the "bad" database the same way they are treating hte "good" one. Key is that this has to be done in SQL, as I need to run this query externally using OleDBCommand from an external tool.

Help any one?

Thanks,

Markus



Answer : Transposing Columns to Rows in Access using SQL

select u.*
from
(
select ID,Name, RepPool, Bank, ParmOne as Parm, ParmValueOne as ParmValue
from yourtable
union
select ID,Name, RepPool, Bank, ParmTwo, ParmValueTwo
from yourtable
union
select ID,Name, RepPool, Bank, ParmThree, ParmValueThree
from yourtable
union
select ID,Name, RepPool, Bank, ParmFour, ParmValueFour
from yourtable
union
select ID,Name, RepPool, Bank, ParmFive, ParmValueFive
from yourtable
) as u
order by u.ID,u.Name, u.RepPool, u.Bank, u.Parm, u.ParmValue
Random Solutions  
 
programming4us programming4us