Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert Columns to Row in SQL


Convert Columns to Row in SQL

Author
Message
jeganbenitto.francis
jeganbenitto.francis
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8970 Visits: 19021
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
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14957 Visits: 38958
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

jeganbenitto.francis
jeganbenitto.francis
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 113
ChrisM@Work ,

Thanks for your reply. I am getting below Error

Ambiguous column name colEEEE .
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8970 Visits: 19021
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search