Here is my base table and i am trying to have a single record for each "app_id".
Ex: "XYZ" has 3 records and would like to convert the same to single record and copy the values to the newly added columns (2ndrank_app_status, 2ndrank_balance, 3rdrank_app_status, 3rdrank_balance)
app_id app_status balance rank
xyz 1 500 1
xyz 1 200 2
xyz 1 200 3
abc 2 300 1
abc 9 200 2
klm 1 800 1
Expected output
app_id app_status balance rank 2ndrank_app_status 2ndrank_balance 3rdrank_app_status 3rdrank_balance
xyz 1 500 1 1 200 1 200
abc 2 300 1 9 200 null null
klm 1 800 1 null null null null
2
Answers
You can simple self join your table a couple times, and get desired outcome.
You can do it using
GROUP BY
andMAX CASE WHEN
:Demo here