Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hello,I'm new to SQL. I have a Table, TableA that has 500 columns in the following format:Timestamp |Data_VAL0|Data_VAL1|Data_VAL2|Data_VAL3|...2010-05-03 10:16:40.003| 15174 | 15173 | 15172 |15171 |... I'd like to take all of the column data excluding the Timestamp column, and make one column - in another table, TableB. AllData 15174151731517215171...I've looked into the Pivot method with no success.Can someone point me in the right direction or provide some sample code?Thanks
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2010-05-03 : 16:52:48
Something like this.
select [Timestamp],Data,Valsfrom(select * from @t) pvtunpivot(Vals for Data IN (Data_VAL0,Data_VAL1,Data_VAL2,Data_VAL3)) unpvt
Cool, this works. But if I have 500+ columns I don't want to write the name of each column in the unpivot command (Vals for Data IN (Data_VAL0,Data_VAL1,Data_VAL2,Data_VAL3,....,Data_VAL500)) unpvtCan i write some sort of for loop to include all Data_VAL### columns?If it helps, I can get rid of the time stamp column and put it into another table, I can join the two tables later.I would attempt to redesign the table, but I am gathering high speed data and transferring it into SQL, in chunks [arrays] to minimize network traffic; otherwise, the computer resources are completely drainedThanks again