0

I have a table

Col A     Col B
===============
1            a
1            b
1            c
2            a
2            b
3            a

And I want it to look like this:

Col A   Col B    Col C   Col D  
===============================
1       a         b         c
2       a         b
3       a

Of course this is highly simplified. I would like this for any N number of distinct variations of Column B values with any M number of distinct variations for column A values.

This is for MS-SQL 2014 if that helps.

Thank You for your time.

Herman
  • 300
  • 2
  • 9
  • What do you do if there's several similar rows since you don't want aggregation? – James Z Nov 11 '15 at 19:36
  • I guess that's what I mean by grouping. I know how to do this if Column B was an integer using pivot. I would sum or count etc.. which is what I meant by aggregation.. sorry for the lack of clarity – Herman Nov 11 '15 at 19:46
  • 1
    Search for dynamic pivot, there should be a lot of examples – James Z Nov 11 '15 at 19:49
  • 3
    with non integer fields just use min or max – JamieD77 Nov 11 '15 at 20:06

1 Answers1

2

You can still use a pivot for this:

Select ColA
  , a AS [Col B]
  , b AS [Col C]
  , c AS [Col D]
 From have
 pivot
 ( max(ColB) for ColB in ([a], [b], [c])
 ) pvt

See SQLFiddle

or see here for doing this dynamically.

Community
  • 1
  • 1
ander2ed
  • 1,318
  • 1
  • 11
  • 19