PixelMuse
PixelMuse

Reputation: 470

Pivoting data using SQL 2008

I'm having some trouble with the syntax on this.

SELECT a.[Id]
  ,a.[Ext]
  ,a.[Title]
  ,b.Text
  FROM [Sliders] a
  LEFT OUTER JOIN SliderItems b on b.SliderId = a.Id

That returns a dataset like this:

Id                                    Ext   Title       Text
1140E5F9-7ABB-42D6-B054-9D30461FC4A9 .jpg Test me big asdftyrthg
1140E5F9-7ABB-42D6-B054-9D30461FC4A9 .jpg Test me big asdfd22sdf
1140E5F9-7ABB-42D6-B054-9D30461FC4A9 .jpg Test me big asdf342324543
1140E5F9-7ABB-42D6-B054-9D30461FC4A9 .jpg Test me big asdf;l;po09

What I really need to do is pivot the data out so that the different text items are each in their own column.

Thanks.

Edit: I think this is closer, but it is telling me the column ID is specified multiple times, can't get rid of that one.

SELECT *
FROM
(SELECT Id, Title FROM Sliders)AS a
LEFT JOIN SliderItems s on (s.SliderId = a.Id)
PIVOT
(
  MAX(s.Text)
FOR s.Text IN ([One],[Two],[Three],[Four])
) AS p

Upvotes: 1

Views: 212

Answers (1)

Anthony Faull
Anthony Faull

Reputation: 17957

SELECT *
FROM
(
  SELECT a.Id, a.Title, b.Text
  FROM dbo.Sliders AS a
  LEFT JOIN dbo.SliderItems b on b.SliderId = a.Id
) AS s
PIVOT
(
  MAX(s.Text)
  FOR s.Text IN ([One],[Two],[Three],[Four])
) AS p

Upvotes: 1

Related Questions