下面显示的查询选择所有活动作业,并在每个阶段的单元格中显示以下内容
EstimatedTime / ActualTime.
(HH:MM)
当 ActualTime 和 EstimatedTime 是 time(7) 时,这确实有效,但我已经将其类型更改为 BigInt 并存储 TimeSpan.Ticks;
如何修改此查询以根据上述格式显示?它必须在 SQL 级别上完成,而不是在显示时完成,因为数据网格准确显示了 SQL 引入的内容。
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' + ID + ',' + QUOTENAME(Name)
FROM JobPhases
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
SELECT @query = 'SELECT *
FROM
(
SELECT j.JobID as ''JobNumber'', c.Registration as ''Reg.'', p.Name,
CONVERT(VARCHAR(8), CAST(DATEADD(ms, SUM(DATEDIFF(ms, ''0:00:00'', j.EstimatedTime)), ''00:00:00'') AS TIME), 108) + ''/'' +
CONVERT(VARCHAR(8), CAST(DATEADD(ms, SUM(DATEDIFF(ms, ''0:00:00'', j.ActualTime)), ''00:00:00'') AS TIME), 108) as [x]
FROM JobDetails AS j
INNER JOIN JobPhases p ON p.ID = j.PhaseId
INNER JOIN Jobs job on job.ID = j.JobID
INNER JOIN Cars c on job.CarID = c.ID
WHERE (job.Status = 1 or job.Status = 0)
GROUP BY c.Registration, p.Name, j.JobID
) JobDetails
PIVOT
( MAX(x)
FOR Name IN (' + @cols + ')
) pvt'
execute(@query);
非常感谢!