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

すべて正しいですが、時間:分:秒として表示する時間をフォーマットする方法を理解するのに助けが必要です...実際には、秒は必要ありません。時間と分だけですが、アドバイス、ヘルプ、指摘正しい方向は素晴らしいでしょう。ありがとうございました!

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 に答える