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

How to transpose 1 column to 1 row Expand / Collapse
Author
Message
Posted Thursday, June 21, 2012 4:12 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 06, 2013 11:02 AM
Points: 2, Visits: 23
Hey guys,

I am a little stumped. I am trying to pull data from one column(could have anywhere from 1-20 distinct values) and transpose that into columns.

For example - I have a table called 'items' with a column named 'codes' - field is a varchar(3)

If I run the following query - select distinct codes from items - I get the following:

Codes
------
1D2
5D3
6H2
4D2

What I would like to do is transpose this so that I get something similiar to this:

Code1 code2 code3 code4
1D2 5D3 6H2 4D2

I have looked at the pivot - but the examples I saw how multiple columns. I just need to transpose the 1. Any ideas?
Post #1319708
Posted Thursday, June 21, 2012 5:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, January 18, 2013 10:14 AM
Points: 8, Visits: 97
If 20 is a hard number then something like this will get you what you are after.


CREATE TABLE #cds (codes VARCHAR(10))

INSERT INTO #cds
( codes )
VALUES ( '1D2'), ('5D3'), ('6H2'), ('4D2')

SELECT Code01, Code02, Code03, Code04, Code05, Code06, Code07, Code08, Code09, Code10,
Code11, Code12, Code13, Code14, Code15, Code16, Code17, Code18, Code19, Code20
FROM (
SELECT TOP 20 'Code'+RIGHT('0'+CAST(ROW_NUMBER() OVER (ORDER BY codes) AS VARCHAR(10)), 4) num, codes
FROM #cds ORDER BY codes
) AS pvt
PIVOT (MAX(codes) FOR num IN (Code01, Code02, Code03, Code04, Code05, Code06, Code07, Code08, Code09, Code10,
Code11, Code12, Code13, Code14, Code15, Code16, Code17, Code18, Code19, Code20)) pt

Post #1319725
Posted Thursday, June 21, 2012 5:45 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 06, 2013 11:02 AM
Points: 2, Visits: 23
That is an awesome and impressive query. Thanks for the help!
Post #1319728
Posted Friday, June 22, 2012 7:39 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 8,620, Visits: 8,261
Take a look at the links in my signature about cross tabs. The first one covers PIVOT and some alternative ways to do the same thing. The second one talks about how to handle this type of thing when you don't know how many columns you will end up with.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1319937
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse