I am looking for an efficient way to convert rows to columns in SQL server using Pivot. I already got the output using aggregate function with cases and joins separately but I am hoping that PIVOT is a clear way to deal with this kind of rows to column transformation.
The example data is
-------------------------------------
| ID | Row | First |Last | Postal |
------------------------------------
| 1 | 1 | John | Doe | B4K |
| 1 | 2 | Matt | Kev | 2H1 |
| 2 | 1 | Hary | Lot | L26 |
| 2 | 2 | Fork | Har | M3R |
| 3 | 1 | Yuv | Hal | L39 |
------------------------------------
This is my result:
| ID | First1 |Last1 | Postal1 | First2 |Last2 | Postal2 |
-----------------------------------------------------------
| 1 | John | Doe | B4K | Matt | Kev | 2H1 |
| 2 | Hary | Lot | L26 | Fork | Har | M3R |
| 3 | Yuv | Hal | L39 | NULL | NULL | NULL |
-----------------------------------------------------------
How could I achieve this result using PIVOT?
Pivot