把我的头等同起来。 这里的一些专家或许会看到我做错了什么?
我有以下时间安排:
CREATE TABLE TimeSheetTime(
TimeSheetTimeID int IDENTITY(1,1) NOT NULL,
TimeSheetItemID int NOT NULL,
OffsetToEntryDate tinyint NOT NULL,
Hours float NOT NULL
)
这些数据包括:
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
Now I d like to return a dataset that has TimeSheetItemID followed by the PIVOT SUM of Hours, arranged in columns of OffsetToEntryDate.
ie :
TimeSheetItemID | 0 | 1 | 2 | 3 | 4 | 5 | 6
我尝试如下:
SELECT *
FROM TimeSheetTime
pivot
(
SUM(Hours)
FOR OffSetToEntryDate
IN (0,1,2,3,4,5,6)
) as TST
欢迎任何建议。