Need Help in how to write a sql query to get output as follows
Table
DP SICS_No UY LOB Rate
TR 0003468A 2006 20 30
TR 0003468A 2006 14 27
TR 0003468A 2006 93 17
TR 0003468A 2006 A2 9
TR 0003468A 2006 52 17
Output
DP SICS_NO UY LOB RATE LOB RATE LOB RATE LOB RATE
TR 0003468A 2006 20 30 14 27 93 17 A2 9
Please let me know on how to proceed. I am trying with pivot but it is not working
This was what i was trying
SELECT *
FROM (
SELECT DP, SICS_No, UY, LOB, Rate
FROM #temp
) AS source_table
PIVOT (
MAX(Rate)
FOR LOB IN ([20], [14], [93], [A2])
) AS pivot_table;

Amit MohantyPosted Jun 13, 2024, 9:47 AM
Check this:
Lokendra SinghPosted Jun 13, 2024, 9:27 AM
Output:
The NumberedRows CTE assigns a unique row number to each combination of DP, SICS_No, and UY, ordered by LOB. This is done using the ROW_NUMBER() window function.
This row number (RowNum) helps in transposing the rows into columns.
The main query uses MAX() combined with CASE statements to pivot the rows into columns. MAX() is used to collapse multiple rows into a single row per group.