1

I have this table:

 id   Name       Value
 ----------------------
 1    Apple      Mac
 2    Apple      iPad
 1    Microsoft  Surface
 2    Microsoft  XBox

I need this:

id   Apple      Microsoft
-------------------------
1    Mac        Surface
2    iPad       XBox

However I won't always know what the 'Name' or 'Value' column data will be. But it will always be in the same column numbers in my select statement

I've tried using pivot but I can't wrap my head around how this would work without aggregate functions

user2884789
  • 373
  • 1
  • 8
  • 21
  • Usually you still use an aggregate, but you select something like "Max" and know that nothing is truly being aggregated. – AaronLS Apr 02 '15 at 16:03
  • Such as here: http://stackoverflow.com/a/27569551/84206 – AaronLS Apr 02 '15 at 16:14
  • Here's another example: http://stackoverflow.com/a/10026046/84206 – AaronLS Apr 02 '15 at 16:15
  • Note the column headers are the `FOR` clause and the values in the cells are the `Max([Name])`. – AaronLS Apr 02 '15 at 16:17
  • possible duplicate of [How to pivot text columns in SQL Server?](http://stackoverflow.com/questions/10025934/how-to-pivot-text-columns-in-sql-server) – AaronLS Apr 02 '15 at 16:18
  • 1
    Select ID,Microsoft,Apple FROM ( select * from Prod ) As Temp PIVOT ( max([value]) FOR [Name] In([Microsoft],[Apple]) ) As pt – mohan111 Apr 02 '15 at 16:36

0 Answers0