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

Convert Columns to Row in SQL Expand / Collapse
Author
Message
Posted Monday, December 16, 2013 7:07 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 8, 2014 10:30 PM
Points: 32, Visits: 113
Can Anyone please help me in below requirement

i have a row as below

1 AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH

I want the output as follows

AAAA BBBB CCCC DDDD
EEEE
FFFF
GGGG
HHHH


But i dont want to use UNPIVOT..


Thanks in advance

Post #1523204
Posted Monday, December 16, 2013 7:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:44 AM
Points: 6,786, Visits: 13,995
SELECT ColAAAA, ColBBBB, ColCCCC, ColDDDD, ColEEEE
FROM Sometables
CROSS APPLY (
SELECT colEEEE UNION ALL
SELECT colFFFF UNION ALL
SELECT colGGGG UNION ALL
SELECT colHHHH
) x


“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 #1523210
Posted Monday, December 16, 2013 7:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 12,905, Visits: 32,161
jeganbenitto.francis (12/16/2013)
Can Anyone please help me in below requirement

i have a row as below

1 AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH

I want the output as follows
AAAA BBBB CCCC  DDDD
EEEE
FFFF
GGGG
HHHH

But i dont want to use UNPIVOT..


Thanks in advance



the tool to use is the unpivot operator.
if you don't want to use it, then you could try using a stack of unions:

your fake data obscure the real issue a lot, hopefully this points you in the right direction.

SELECT AAAA,BBBB,CCCC,DDDD As DDDD UNION ALL
SELECT AAAA,BBBB,CCCC,EEEE As DDDD UNION ALL
SELECT AAAA,BBBB,CCCC,FFFF As DDDD UNION ALL
SELECT AAAA,BBBB,CCCC,GGGG As DDDD UNION ALL
SELECT AAAA,BBBB,CCCC,HHHH As DDDD



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1523211
Posted Monday, December 16, 2013 7:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 8, 2014 10:30 PM
Points: 32, Visits: 113
ChrisM@Work ,

Thanks for your reply. I am getting below Error

Ambiguous column name colEEEE .
Post #1523220
Posted Monday, December 16, 2013 7:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:44 AM
Points: 6,786, Visits: 13,995
jeganbenitto.francis (12/16/2013)
ChrisM@Work ,

Thanks for your reply. I am getting below Error

Ambiguous column name colEEEE .

Without a table name or column names to work with, I made up the names of the columns used in the query. Substitute your actual table name and column names.
Somewhere in my query you will have to name the output column - either the first select as a column alias, or after the table alias.


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

Add to briefcase

Permissions Expand / Collapse