Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

Pivotting two columns using dynamic sql

Language: Layout:
View schema
Execution time: 0,02 sec, rows selected: 7, rows affected: 12, absolute service time: 0,17 sec 
latest |  history
   (No column name)
1 select No , [Code_1] = max(case when rn = 1 then Code end) , [Amount1] = max(case when rn = 1 then Amount end) , [Code_2] = max(case when rn = 2 then Code end) , [Amount2] = max(case when rn = 2 then Amount end) , [Code_3] = max(case when rn = 3 then Code end) , [Amount3] = max(case when rn = 3 then Amount end) , [Code_4] = max(case when rn = 4 then Code end) , [Amount4] = max(case when rn = 4 then Amount end) , [Code_5] = max(case when rn = 5 then Code end) , [Amount5] = max(case when rn = 5 then Amount end) from ( select * , rn = row_number() over (partition by No order by Code) from Table1 ) sub group by No
   No Code_1 Amount1 Code_2 Amount2 Code_3 Amount3 Code_4 Amount4 Code_5 Amount5
1 1 23 95,79 61 45,00 61 1714,00 80 31,00 NULL NULL
2 2 23 95,79 61 1714,00 NULL NULL NULL NULL NULL NULL
3 3 80 31,00 NULL NULL NULL NULL NULL NULL NULL NULL
4 4 23 95,79 61 45,00 61 45,00 61 1714,00 80 31,00

    
                
λ
.NET NoSQL database for rapid development