-1

How would I do the pivot below?

I have a table like this:

+------+---+----+
| round| id| kpi|
+------+---+----+
| 0    | 1 | 0.1|
| 1    | 1 | 0.2|
| 0    | 2 | 0.5|
| 1    | 2 | 0.4|
+------+---+----+

I want to convert the id column into multiple columns (same amount of different ids), with KPI value as their values and in the new table we keep the rounds like in the first table.

+------+----+----+
| round| id1| id2|
+------+----+----+
| 0    | 0.1| 0.5|
| 1    | 0.2| 0.4|
+------+----+----+

Is it possible to do this in SQL? How to do that?

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Thaise
  • 965
  • 3
  • 14
  • 25
  • As per the question guide, please show what you’ve tried and tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. And please only tag a single RDBMS. MySQL <> SQL Server <> PostgreSQL. Some form of pivot is what you are looking for. – Dale K Jul 06 '21 at 03:50
  • What if you have thousands of different IDs? – a_horse_with_no_name Jul 06 '21 at 06:20

1 Answers1

0

You are looking for a pivot function. You can find details on how to do this here and here. The first link also provides input into how to do this if you have an unknown number of columnnames.

AlexanderP
  • 106
  • 6