2

请帮忙。这里有一点问题,需要一些帮助来解决。我有一个返回多行的查询,当我尝试格式化要显示的时间时,我会收到以下消息...

消息 130,级别 15,状态 1,第 3 行无法对包含聚合或子查询的表达式执行聚合函数。

这是我收到错误时使用的格式...

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)

当我自己在查询中运行它时,我得到了我想要的正确格式......

Project Name     Total Time
AAA               0:00:19
BBB               0:00:04
CCC               6:00:06
DDD               16:05:52

但是,我似乎无法弄清楚如何格式化以下查询以这种方式显示时间......

SELECT ProjName AS 'Project Name', 
       CONVERT(VARCHAR(10), SUM(CASE WHEN Phone = 0 AND Research = 0 
                                     THEN (DATEDIFF(SECOND, 0, EntryTime))  
                                END)) AS [Total Time No PMRE], 
       CONVERT(VARCHAR(10), SUM(CASE WHEN Phone = 1 AND Research = 0 
                                     THEN (DATEDIFF(SECOND, 0, EntryTime))  
                                END)) AS [Total Time Phone], 
       CONVERT(VARCHAR(10), SUM(CASE WHEN Phone = 0 AND Research = 1 
                                     THEN (DATEDIFF(SECOND, 0, EntryTime)) 
                                END)) AS [Total Time Research], 
       CONVERT(VARCHAR(10), SUM(DATEDIFF(SECOND, 0, EntryTime)) ) AS 'Total Time'
FROM WorkTime
WHERE EntryTime IS NOT NULL
GROUP BY ProjName

并得到以下结果...

Project Name  Total Time No PMRE  Total Time Phone  Total Time Research  Total Time
AAA           19                  NULL              NULL                 19
BBB           4                   NULL              NULL                 4
CCC           10800               7200              3606                 21606
DDD           57952               NULL              NULL                 57952

所有这些都是正确的,但我需要帮助来弄清楚如何格式化时间以显示为 hrs:mins:secs ... 实际上,我不需要秒,只需要小时和分钟,但任何建议、帮助、指向正确的方向会很棒。谢谢!

4

1 回答 1

2

尝试:

with cte as
(SELECT ProjName , 
        SUM(CASE WHEN Phone = 0 AND Research = 0 THEN DATEDIFF(SECOND, 0, EntryTime) END) AS [Total Time No PMRE], 
        SUM(CASE WHEN Phone = 1 AND Research = 0 THEN DATEDIFF(SECOND, 0, EntryTime) END) AS [Total Time Phone], 
        SUM(CASE WHEN Phone = 0 AND Research = 1 THEN DATEDIFF(SECOND, 0, EntryTime) END) AS [Total Time Research], 
        SUM(DATEDIFF(SECOND, 0, EntryTime)) AS [Total Time]
 FROM WorkTime
 WHERE EntryTime IS NOT NULL
 GROUP BY ProjName)
SELECT ProjName AS [Project Name], 
       CAST([Total Time No PMRE] / 3600 AS varchar(12)) + ':'
       + RIGHT('0' + CAST([Total Time No PMRE] / 60 % 60 AS varchar(2)), 2) + ':'
       + RIGHT('0' + CAST([Total Time No PMRE] % 60 AS varchar(2)), 2) AS [Total Time No PMRE], 
       CAST([Total Time Phone] / 3600 AS varchar(12)) + ':'
       + RIGHT('0' + CAST([Total Time Phone] / 60 % 60 AS varchar(2)), 2) + ':'
       + RIGHT('0' + CAST([Total Time Phone] % 60 AS varchar(2)), 2) AS [Total Time Phone], 
       CAST([Total Time Research] / 3600 AS varchar(12)) + ':'
       + RIGHT('0' + CAST([Total Time Research] / 60 % 60 AS varchar(2)), 2) + ':'
       + RIGHT('0' + CAST([Total Time Research] % 60 AS varchar(2)), 2) AS [Total Time Research], 
       CAST([Total Time] / 3600 AS varchar(12)) + ':'
       + RIGHT('0' + CAST([Total Time] / 60 % 60 AS varchar(2)), 2) + ':'
       + RIGHT('0' + CAST([Total Time] % 60 AS varchar(2)), 2) AS [Total Time]
FROM cte
于 2013-05-14T16:38:40.077 回答