Create table test2 (Id int,name varchar (), quarter char,profile1 int) insert into test2 values (1, ' a ', 1,1000) insert into test2 values (1, ' a ', 2,2000) insert into test2 values (1, ' a ', 3,4000) insert into test2 values (1, ' a ', 4,5000) insert into test2 values (2, ' B ', 1,3000) insert into test2 values (2, ' B ', 2,3500) insert into test2 values (2, ' B ', 3,4200) insert into test2 values (2, ' B ', 4,5500) Select * from test2select id,name,[1] as Quarter,[2] as Two quarter,[3] as three quarter ,[4] as Four Quarter From test2pivot (count ([Profile1]) for [quarter] in ([1],[2],[3],[4])) pvt /*id name quarter profile1----------- -------------------- ------- -----------1 a 1 10001 a 2 20001 a 3 40001 a 4 50002 b 1 30002 b 2 35002 b 3 42002 b 4 5500 (8 line affected) id name First quarter two quarter three quarter Four quarterly----------- -------------------- ----------- ----------- ----------- -----------1 a 1 1 1 12 b 1 1 1 1 (2 line affected) */
SQL Server row to column