1. Pivot usage (row to column)
Select * from as apivot (sum for in ([ language ],[ Math ],[ Foreign Language ], [ Literature ],[ Comprehensive Management ] as B
Data:
ID name score1 Chinese 2 math 3 Foreign Languages 4 of 5 Comprehensive 300
Results:
ID Chinese Mathematics Foreign Language comprehensive comprehensive theory1 - NULL NULL NULL NULL2 NULL the NULL NULL NULL3 NULL NULL - NULL NULL4 NULL NULL NULL - NULL5 NULL NULL NULL NULL -
2. Unpivot usage (career change)
Select from (Selectfrom[dbo]. [table_week] for in as B
Data:
week1 week2 week3 week4 week5 One A - - - A - - - the - - - the the - - the the the
Results:
Orders Weekend OneWeek1 AWeek2 -Week3 -Week4 -Week5 AWeek1 -Week2 -Week3 -Week4 theWeek5 -Week1 -Week2 -Week3 theWeek4 theWeek5 -Week1 -Week2 theWeek3 theWeek4 theWeek5
Usage of SQL pivot (row to column) and Unpivot (column change)