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

Pivotting two columns using dynamic sql

Language: Layout:
View schema
Execution time: 0,03 sec, rows selected: 7, rows affected: 12, absolute service time: 0,2 sec 
edit mode |  history
   (No column name)
1 select No , [Code_1] = max(case when rn = 1 then Code end) , [Amount_1] = max(case when rn = 1 then Amount end) , [Code_2] = max(case when rn = 2 then Code end) , [Amount_2] = max(case when rn = 2 then Amount end) , [Code_3] = max(case when rn = 3 then Code end) , [Amount_3] = max(case when rn = 3 then Amount end) , [Code_4] = max(case when rn = 4 then Code end) , [Amount_4] = max(case when rn = 4 then Amount end) , [Code_5] = max(case when rn = 5 then Code end) , [Amount_5] = max(case when rn = 5 then Amount end) from ( select No, Code, Amount , rn = row_number() over (partition by No order by Code) from Table1 ) sub group by No
   No Code_1 Amount_1 Code_2 Amount_2 Code_3 Amount_3 Code_4 Amount_4 Code_5 Amount_5
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