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 Tuesday, July 8, 2014 5:34 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, November 13, 2014 9:23 PM
Points: 86, Visits: 223
yes, ChrisM's query is simple aswell it is more efficient and Optimized too..

--Estimated Subtree Cost = 0.0034222 (to calculate 21.6 rows)
select Field from #tempunpivot UNPIVOT (Field for ColumnName IN ([unic],[nrc],[nr_tel],[id_stud])) unpvt

--Estimated Subtree Cost = 0.0034131 (to Calculate 24 rows)
SELECT NewColumn
FROM #tempunpivot t
CROSS APPLY (VALUES (unic), (nrc), (nr_tel), (id_stud)) d (NewColumn)

I would like to stick with "Cross Apply", Query thanks to ChrisM & Mouris..

Regards,
Prabhu
Post #1590305
Posted Tuesday, July 8, 2014 5:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:58 AM
Points: 6,863, Visits: 14,161
prabhu.st (7/8/2014)
yes, ChrisM's query is simple aswell it is more efficient and Optimized too..

--Estimated Subtree Cost = 0.0034222 (to calculate 21.6 rows)
select Field from #tempunpivot UNPIVOT (Field for ColumnName IN ([unic],[nrc],[nr_tel],[id_stud])) unpvt

--Estimated Subtree Cost = 0.0034131 (to Calculate 24 rows)
SELECT NewColumn
FROM #tempunpivot t
CROSS APPLY (VALUES (unic), (nrc), (nr_tel), (id_stud)) d (NewColumn)

I would like to stick with "Cross Apply", Query thanks to ChrisM & Mouris..

Regards,
Prabhu


CROSS APPLY VALUES may or may not be quicker than UNPIVOT. There's usually not much in it - and CAV is usually easier to write and test. Dwain Camps has more details of the two methods in this excellent article.


“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 #1590307
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse