Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Transpose rows into one column Expand / Collapse
Author
Message
Posted Monday, July 7, 2014 2:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 6:30 AM
Points: 10, Visits: 62
Hi guys,

I have a table with a lot of data like in the example:

unic | nrc | nr_tel | id_stud
-----+-----+--------+---------
2343 | 123 | 354354 | 123
1231 | 432 | 534523 | 324

It has 30k rows like this.
I want to transpose each row into one column. The output should be like:

data
-----+
2343
123
354354
123
1231
432
534523
324

How can I do this ?

Thanks!
Post #1589777
Posted Monday, July 7, 2014 3:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 13,261, Visits: 10,140
Take a look at UNPIVOT.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1589783
Posted Monday, July 7, 2014 3:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 6:30 AM
Points: 10, Visits: 62
I solved it. Yes, the unpivot was the answer. I used the following query :

select Field from myTable UNPIVOT (Field for ColumnName IN ([unic],[nrc],[nr_tel],[id_stud])) unpvt

Thanks!
Post #1589787
Posted Tuesday, July 8, 2014 12:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 6:30 AM
Points: 10, Visits: 62
Is there anyway to keep the NULL data when UNPIVOT ?
Post #1590192
Posted Tuesday, July 8, 2014 1:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 13,261, Visits: 10,140
http://stackoverflow.com/questions/1002989/sql-server-include-null-using-unpivot



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1590205
Posted Tuesday, July 8, 2014 2:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 6:30 AM
Points: 10, Visits: 62
I have just read that and tried some queries but I can`t obtain the right result.

I`ll apreciate a little help
Post #1590232
Posted Tuesday, July 8, 2014 2:35 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 13,261, Visits: 10,140
MariusC (7/8/2014)
I have just read that and tried some queries but I can`t obtain the right result.

I`ll apreciate a little help


How about some sample data and desired output?
Check the first link in my signature on how to do this.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1590234
Posted Tuesday, July 8, 2014 2:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 6:30 AM
Points: 10, Visits: 62
--===== Create the test table with

create table #test
(
id_respondent integer,
projid integer,
gfkroid integer
)


--===== Insert the test data into the test table

insert into #test (id_respondent,gfkroid) values (1234,4567),(2345,4546),(3255,2345)



How the data looks like:

id_respondet | projid | gfkroid
-------------+--------+---------+
1234 | NULL | 4567
2345 | NULL | 4546
3255 | NULL | 2345

How is the desire output:

New_row |
--------+
1234
NULL
4567
2345
NULL
4546
3255
NULL
2345
Post #1590238
Posted Tuesday, July 8, 2014 4:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:56 AM
Points: 7,120, Visits: 13,491
SELECT NewColumn 
FROM #test t
CROSS APPLY (VALUES (id_respondent), (projid), (gfkroid)) d (NewColumn)



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1590267
Posted Tuesday, July 8, 2014 4:20 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 6:30 AM
Points: 10, Visits: 62
Hmm it was that simple

Thank you very much !
Post #1590275
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse