SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pivot multiple columns into one row


Pivot multiple columns into one row

Author
Message
Uji2
Uji2
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
I have a temp table that looks like this:



I need to pivot the values in TrackName, TrackStatus, and LastEvalDate into one row for each CrewMemberNotesID so that it looks like this:



Currently I'm doing this with 3 separate pivot statements - one for each column that I'm pivoting (TrackName, LastEvalDate, TrackStatus).
My code looks like this:


-- tracks
;WITH TrackName
AS
(
SELECT cs.CrewMemberNotesID,
cs.SortOrder,
cs.TrackName
FROM #tmp_crew_status cs
)
INSERT #tmp_crew_status_pivoted
(
CrewMemberNotesID,
Track1_Name,
Track2_Name,
Track3_Name,
Track4_Name,
Track5_Name,
Track6_Name,
Track7_Name,
Track8_Name
)
SELECT CrewMemberNotesID = CrewMemberNotesID,
Track1_Name = [1],
Track2_Name = [2],
Track3_Name = [3],
Track4_Name = [4],
Track5_Name = [5],
Track6_Name = [6],
Track7_Name = [7],
Track8_Name = [8]
FROM TrackName PIVOT (MAX(TrackName) FOR SortOrder IN ([1], [2], [3], [4], [5], [6], [7], [8])) AS pvt


Is there a way to do this in one statement without writing dynamic sql?
Thanks for the help!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (896K reputation)SSC Guru (896K reputation)SSC Guru (896K reputation)SSC Guru (896K reputation)SSC Guru (896K reputation)SSC Guru (896K reputation)SSC Guru (896K reputation)SSC Guru (896K reputation)

Group: General Forum Members
Points: 896590 Visits: 48275
Uji2 - Thursday, December 6, 2018 8:29 AM
Is there a way to do this in one statement without writing dynamic sql?
Thanks for the help!
Yes. It's actually pretty simple. See the following article...
http://www.sqlservercentral.com/articles/T-SQL/63681/


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Uji2
Uji2
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
Thanks Jeff! That's exactly what I was looking for.
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