这里很新,真的可以使用一些帮助。我有一张桌子
TimeID(pk), DateEntry(date), EntryTiem(time),
ProjID(int), ProjName(varchar), Phone(bit),
Research(bit), Notes(varchar), ProjActive(bit),
TimeDateStamp(date).
我正在尝试将多行数据返回到每个 ProjName 但没有任何运气。这是我到目前为止想出的...
SELECT ProjName AS 'Project Name',
(SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
+ ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)
FROM WorkTime
WHERE Phone = 0 AND Research = 0 AND EntryTime IS NOT NULL
) AS 'Total Time No PMRE',
(SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
+ ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)
FROM WorkTime
WHERE Phone = 1 AND RESEARCH = 0 AND EntryTime IS NOT NULL
) AS 'Total Time Phone',
(SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
+ ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)
FROM WorkTime
WHERE Phone = 0 AND RESEARCH = 1 AND EntryTime IS NOT NULL
) AS 'Total Time Research',
(SELECT CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 3600 AS varchar(12)) + ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) / 60 % 60 AS varchar(2)),2)
+ ':' + RIGHT('0' + CAST(SUM(DATEDIFF(second, 0, EntryTime)) % 60 AS varchar(2)), 2)
FROM WorkTime
WHERE EntryTime IS NOT NULL
) AS 'TotalTime'
FROM WorkTime
WHERE EntryTime IS NOT NULL
GROUP BY ProjName
它返回以下...
Project Name Total Time No PMRE Total Time Phone Total Time Research Total Time
AAAA 19:06:15 2:00:00 1:00:06 22:06:21
BBBB 19:06:15 2:00:00 1:00:06 22:06:21
CCCC 19:06:15 2:00:00 1:00:06 22:06:21
DDDD 19:06:15 2:00:00 1:00:06 22:06:21
这是错误的。每列中的数量是该条件的总和,所有 ProjName 加在一起而不是单独分离出来。
这就是输出应该是什么......
Project Name Total Time No PMRE Total Time Phone Total Time Research Total Time
AAAA 00:00:19 0:00:00 0:00:00 00:00:19
BBBB 00:00:04 0:00:00 0:00:00 00:00:04
CCCC 03:00:00 2:00:00 1:00:06 06:06:06
DDDD 16:05:52 0:00:00 0:00:00 16:05:52
有没有人有任何建议或可以帮助我指出正确的方向?谢谢!