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»»

How do I convert column data into row data? Expand / Collapse
Author
Message
Posted Tuesday, September 3, 2013 9:23 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 25, 2014 1:17 PM
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?
Post #1491128
Posted Wednesday, September 4, 2013 7:16 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 10:10 AM
Points: 526, Visits: 1,163
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...
Post #1491279
Posted Wednesday, September 4, 2013 7:19 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 10:10 AM
Points: 526, Visits: 1,163
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...
Post #1491284
Posted Wednesday, September 4, 2013 7:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #1491305
Posted Wednesday, September 4, 2013 8:01 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 10:10 AM
Points: 526, Visits: 1,163
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...
Post #1491323
Posted Wednesday, September 4, 2013 8:06 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #1491324
Posted Wednesday, September 4, 2013 2:50 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, April 25, 2014 1:17 PM
Points: 8, Visits: 41
Great working examples, thanks for the education!
Post #1491542
Posted Wednesday, September 11, 2013 3:54 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:53 PM
Points: 3,438, Visits: 5,390
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!
Post #1493537
Posted Wednesday, September 11, 2013 4:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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


“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 #1493542
Posted Wednesday, September 11, 2013 3:53 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, December 12, 2014 1:02 AM
Points: 823, Visits: 753
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
Post #1493931
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse