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 2005 Forums
 Transact-SQL (2005)
 help for table transformation rows to columns

Author  Topic 

mwidjaja
Starting Member

8 Posts

Posted - 2010-07-03 : 09:50:25
Hi,
How do I transform:
this table:
Col1 Col2
DATA1 A1
DATA1 A2
DATA1 A3
DATA2 B1
DATA2 B2
DATA2 B3
DATA3 C1
DATA3 C2
DATA3 C3

Col1 Col2 Col3 COl4
Data1 A1 A2 A3
Data2 B1 B2 B3
Data3 C1 C2 C3

I used temporary table with something like:
create table ##table (col1 nvarchar(10),col2 float,col3 float,col4 float)
insert ##table
select col1,col2,col3,col4
where
col1 like '%data%'
select * from ##table
drop table ##table

I can only get 1 Data row. Any input is greatly appreciated.
MW

sql-programmers
Posting Yak Master

190 Posts

Posted - 2010-07-04 : 22:56:58
Hi,

I have a blog article to maybe assist you with your question:

http://www.sql-programmers.com/Blog/tabid/153/EntryId/6/Using-PIVOT-and-UNPIVOT.aspx

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

rocknpop
Posting Yak Master

201 Posts

Posted - 2010-07-05 : 00:39:42
can you try this:

SELECT
col1,
MAX(CASE WHEN CHARINDEX(1,col2)>0 THEN col2 END) AS 'col2',
MAX(CASE WHEN CHARINDEX(2,col2)>0 THEN col2 END) AS 'col3',
MAX(CASE WHEN CHARINDEX(3,col2)>0 THEN col2 END) AS 'col4'
FROM table
GROUP BY col1

--------------------
Rock n Roll with SQL
Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2010-07-05 : 01:17:19

I have a blog article to maybe assist you with your question:

http://www.sql-programmers.com/Blog/tabid/153/EntryId/6/Using-PIVOT-and-UNPIVOT.aspx


Hi sql-programmers
I think Pivot involves aggregating process. Could you provide code to solve this problem by using Pivot?

Thanks
Go to Top of Page
   

- Advertisement -