Skip to content
Advertisement

Export data from a non-normalized database

I need to export data from a non-normalized database where there are multiple columns to a new normalized database.
One example is the Products table, which has 30 boolean columns (ValidSize1, ValidSize2 ecc…) and every record has a foreign key which points to a Sizes table where there are 30 columns with the size codes (XS, S, M etc…). In order to take the valid sizes for a product I have to scan both tables and take the value SizeCodeX from the Sizes table only if ValidSizeX on the product is true. Something like this:

For now I am using a “template” query which I repeat for 30 times:

I am just putting this query inside a loop and I replace the “_1” with the loop index:

Is there a better, cleaner or faster method to do this? I am using SQL Server and I can only use SQL/TSQL.

Advertisement

Answer

Well, it seems that a “clean” (and much faster!) solution is the UNPIVOT function.
I found a very good example here:

http://pratchev.blogspot.it/2009/02/unpivoting-multiple-columns.html

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement