Extrayez mes cheveux avec cette requête. Peut-être que certains des experts ici peuvent voir ce que je fais mal?Syntaxe PIVOT dans SQL Server 2005
J'ai un tableau TimeSheetTime comme suit:
CREATE TABLE TimeSheetTime(
TimeSheetTimeID int IDENTITY(1,1) NOT NULL,
TimeSheetItemID int NOT NULL,
OffsetToEntryDate tinyint NOT NULL,
Hours float NOT NULL
)
C'est rempli avec des données comme suit:
INSERT TimeSheetTime (TimeSheetItemID, OffsetToEntryDate, Hours)
SELECT 1,1,7
UNION SELECT 1,3,1.5
UNION SELECT 1,0,1
UNION SELECT 2,1,0
UNION SELECT 2,4,0
UNION SELECT 2,3,0
UNION SELECT 3,2,0
UNION SELECT 3,4,1
UNION SELECT 3,5,2
UNION SELECT 3,1,2
UNION SELECT 3,2,4
UNION SELECT 4,3,5
UNION SELECT 4,5,16
UNION SELECT 4,2,1
UNION SELECT 5,3,0.5
UNION SELECT 5,4,1
UNION SELECT 5,3,1
UNION SELECT 5,4,4
UNION SELECT 6,5,4
UNION SELECT 6,1,0.5
UNION SELECT 6,2,3.2
UNION SELECT 7,3,0.2
UNION SELECT 7,5,1.5
UNION SELECT 7,1,0.1
UNION SELECT 8,3,0.4
UNION SELECT 8,5,0.5
UNION SELECT 8,1,0.3
UNION SELECT 8,3,1
UNION SELECT 9,5,2
UNION SELECT 9,3,0.5
Maintenant, je voudrais revenir un ensemble de données qui a TimeSheetItemID suivi du PIVOT SUM d'heures, disposées dans des colonnes de OffsetToEntryDate. soit:
TimeSheetItemID | 0 | 1 | 2 | 3 | 4 | 5 | 6
J'ai essayé le suivant
SELECT *
FROM TimeSheetTime
pivot
(
SUM(Hours)
FOR OffSetToEntryDate
IN (0,1,2,3,4,5,6)
) as TST
Toutes les suggestions sont appréciés.
Marc. Toi. Sont. Le. Shiz. - Merci pour l'aide. – Joshua