I am a SAS programmer who has basic knowledge of SQL. I am trying to convert SAS programs to SQL scripts and I need help with converting Rows to Columns in SQL Server.
I tried some of the solutions(like PIVOT, Multiple Joins etc.) available in this forum, but could not achieve what I needed. The problem is, the number rows for ID might vary from 1 - 500 (or more) and I need to create these columns dynamically. In SAS however, I do not have to bother about the varying number of rows as SAS will do it automatically using pre-defined procedures/methods (like Proc Transpose or Arrays).
Here is some sample data that I am trying to work with.
Appreciate your help in this regard.
ID STARTDT ENDDT
1 1/1/2020 1/3/2020
1 2/25/2020 2/28/2020
1 3/10/2020 3/15/2020
2 1/1/2020 1/3/2020
2 2/25/2020 2/28/2020
2 3/10/2020 3/15/2020
2 3/20/2020 3/20/2020
2 3/25/2020 3/31/2020
3 1/1/2020 1/3/2020
4 2/25/2020 2/28/2020
4 3/10/2020 3/15/2020
Desired Output.
ID STDT1 ENDT1 STDT2 ENDT2 STDT3 ENDT3 STDT4 ENDT4 STDT5 ENDT5 ........... STDT(MAX) ENDT(MAX)
1 1/1/2020 1/3/2020 2/25/2020 2/28/2020 3/10/2020 3/15/2020
2 1/1/2020 1/3/2020 2/25/2020 2/28/2020 3/10/2020 3/15/2020 3/20/2020 3/20/2020 3/25/2020 3/31/2020
3 1/1/2020 1/3/2020
4 2/25/2020 2/28/2020 3/10/2020 3/15/2020