Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Transforming Data

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-07-13 : 10:14:27
Markus writes "I recently inherited a web site with a SQL back end. One of the functions of the web site is to allow clients to customize fields. There are four major classes, two classes could have up to 8 fields. Two other classes could have up to 50 fields. The original designers simply made an individual column for each possible field, leaving me with a table of over 100 columns, which needless to say, is bad.

SO what I'd want to do is create a second table with each category, related to a client ID. Then I'd have 4 columns and each column could have as many fields as I want.

So in the first table I have one category in 8 columns: TT1, TT2, TT3...TT8. I'd like to take that data and move it to the TT column in a details table. So if a row in the first column has all eight fields filled, I would now have 8 rows in the new table, each with the same clientID, but 1 TT column.

In essence I want to take 1 row with 8 columns and transform it to 8 rows with 1 column. What's the best way to do this? Is it even possible?

Thanks!"

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2004-07-13 : 18:23:44
Easy - use a UNION query.

SELECT Field1 FROM table
UNION
SELECT Field2 FROM table
UNION
SELECT Field3 FROM table
UNION
SELECT Field4 FROM table
UNION
SELECT Field5 FROM table
etc.
etc.

Go to Top of Page
   

- Advertisement -