lunes, 9 de enero de 2012

Creating temporal table, inserting and pivoting

--CREATING TEMPORAL TABLE
DECLARE @Tabla TABLE (
FName VARCHAR(25),
School VARCHAR(15),
Team VARCHAR(15),
Points INT
)

--INSERTING VALUES
INSERT INTO @Tabla VALUES ('Pablo','OLOF','MEN',5)
INSERT INTO @Tabla VALUES ('Pedro','OLOF','MEN',5)
INSERT INTO @Tabla VALUES ('Ruby','OLOF','WOMEN',5)
INSERT INTO @Tabla VALUES ('Ruby','OLOF','WOMEN',5)
INSERT INTO @Tabla VALUES ('Paco','AMAT','WOMEN',5)

--VIEW
SELECT * FROM @Tabla

--PIVOTING VALUES
SELECT School,[MEN],[WOMEN]
FROM (SELECT School,Team,Points FROM @Tabla) As T
PIVOT (SUM(Points)
FOR Team IN ([MEN],[WOMEN])) As PivotT

SELECT FName As FullName,School,[MEN],[WOMEN]
FROM (SELECT Fname,School,Team,Points FROM @Tabla) As T
PIVOT (SUM(Points)
FOR Team IN ([MEN],[WOMEN])) AS PivotT