Look for PIVOT function if you are using SQL Server.