SQL将BigInt'Ticks'转换为时间

时间:2013-08-21 06:27:46

标签: sql sql-server-2008 pivot

下面显示的查询选择所有活动作业,并在每个阶段的单元格中显示以下内容

EstimatedTime / ActualTime.
(HH:MM)

当ActualTime和EstimatedTime为时间(7)时,这确实有效,但我已将其类型更改为BigInt并存储TimeSpan.Ticks;

如何根据上述格式修改此查询以显示?它必须在SQL级别上完成,而不是在显示时,因为datagrid确实显示了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);

非常感谢!

0 个答案:

没有答案