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


How do I convert column data into row data?


How do I convert column data into row data?

Author
Message
gregorykearney
gregorykearney
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
table gennum
tollnum, n1, n2, n3
800123, 1234, 1235, 1236
999123, 9876, 9875, 9874

I want to my data to look like:
tollnum,code
800123, 1234
800123, 1235
800123, 1236
999123, 9876
999123, 9875
999123, 9874

How do I convert column data into row data?
PiMané
PiMané
Mr or Mrs. 500
Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)

Group: General Forum Members
Points: 592 Visits: 1334
gregorykearney (9/3/2013)
table gennum
tollnum, n1, n2, n3
800123, 1234, 1235, 1236
999123, 9876, 9875, 9874

I want to my data to look like:
tollnum,code
800123, 1234
800123, 1235
800123, 1236
999123, 9876
999123, 9875
999123, 9874

How do I convert column data into row data?

Hi,

You can use the UNPIVOT statment:
SELECT tollnum,
code
FROM (SELECT *
FROM gennum) p
UNPIVOT
(code
FOR n IN ( n1, n2, n3 ) ) AS unpvt
ORDER BY tollnum, n



If you need to work better, try working less...
PiMané
PiMané
Mr or Mrs. 500
Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)

Group: General Forum Members
Points: 592 Visits: 1334
gregorykearney (9/3/2013)
table gennum
tollnum, n1, n2, n3
800123, 1234, 1235, 1236
999123, 9876, 9875, 9874

I want to my data to look like:
tollnum,code
800123, 1234
800123, 1235
800123, 1236
999123, 9876
999123, 9875
999123, 9874

How do I convert column data into row data?

Or a simple UNION ALL

SELECT * FROM (
SELECT tollnum, n1 as N FROM gennum
UNION ALL
SELECT tollnum, n2 FROM lixo
UNION ALL
SELECT tollnum, n3 FROM lixo
) t
ORDER BY tollnum

Either way it works...



If you need to work better, try working less...
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: 8957 Visits: 19016
Or CROSS APPLY VALUES:
-- sample data
;WITH gennum (tollnum, n1, n2, n3) AS (
SELECT 800123, 1234, 1235, 1236 UNION ALL
SELECT 999123, 9876, 9875, 9874
)
-- solution
SELECT g.tollnum, d.code
FROM gennum g
CROSS APPLY (VALUES (n1), (n2), (n3)) d (code)



“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
PiMané
PiMané
Mr or Mrs. 500
Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)

Group: General Forum Members
Points: 592 Visits: 1334
ChrisM@Work (9/4/2013)
Or CROSS APPLY VALUES:
-- sample data
;WITH gennum (tollnum, n1, n2, n3) AS (
SELECT 800123, 1234, 1235, 1236 UNION ALL
SELECT 999123, 9876, 9875, 9874
)
-- solution
SELECT g.tollnum, d.code
FROM gennum g
CROSS APPLY (VALUES (n1), (n2), (n3)) d (code)


Didn't know this one :-)
One more for the "bag" :-)
Nice.



If you need to work better, try working less...
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: 8957 Visits: 19016
PiMané (9/4/2013)
ChrisM@Work (9/4/2013)
Or CROSS APPLY VALUES:
-- sample data
;WITH gennum (tollnum, n1, n2, n3) AS (
SELECT 800123, 1234, 1235, 1236 UNION ALL
SELECT 999123, 9876, 9875, 9874
)
-- solution
SELECT g.tollnum, d.code
FROM gennum g
CROSS APPLY (VALUES (n1), (n2), (n3)) d (code)


Didn't know this one :-)
One more for the "bag" :-)
Nice.


Dwain C has a nice article on it, Ped ;-)

“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
gregorykearney
gregorykearney
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 41
Great working examples, thanks for the education!
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
PiMané (9/4/2013)
ChrisM@Work (9/4/2013)
Or CROSS APPLY VALUES:
-- sample data
;WITH gennum (tollnum, n1, n2, n3) AS (
SELECT 800123, 1234, 1235, 1236 UNION ALL
SELECT 999123, 9876, 9875, 9874
)
-- solution
SELECT g.tollnum, d.code
FROM gennum g
CROSS APPLY (VALUES (n1), (n2), (n3)) d (code)


Didn't know this one :-)
One more for the "bag" :-)
Nice.


And after all the work I've done trying to get the word out too! [face-to-palm]

Thanks for the plug Chris!


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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: 8957 Visits: 19016
dwain.c (9/11/2013)
PiMané (9/4/2013)
ChrisM@Work (9/4/2013)
Or CROSS APPLY VALUES:
-- sample data
;WITH gennum (tollnum, n1, n2, n3) AS (
SELECT 800123, 1234, 1235, 1236 UNION ALL
SELECT 999123, 9876, 9875, 9874
)
-- solution
SELECT g.tollnum, d.code
FROM gennum g
CROSS APPLY (VALUES (n1), (n2), (n3)) d (code)


Didn't know this one :-)
One more for the "bag" :-)
Nice.


And after all the work I've done trying to get the word out too! [face-to-palm]

Thanks for the plug Chris!


I felt sorry for you tramping the streets of Port Moresby with that heavy sandwich board Hehe

“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
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)

Group: General Forum Members
Points: 931 Visits: 866
ChrisM@Work (9/4/2013)
Or CROSS APPLY VALUES


Somewhat odd to use CROSS APPLY here. The normal is CROSS JOIN. Of course since there is no correlation on the right side, the CROSS APPLU is effectively a CROSS JOIN, but nevertheless.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
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