从DateDiff更改int到Time

时间:2013-11-27 14:08:57

标签: sql sql-server time int datediff

我有3次将其放在条形图上但我无法解决如何将AvgUserSegmentTime更改为时间格式(00:00:00.00)。现在我得到Fastest = 00:00:00.01 Slowest = 00:00:24:22.22和Avg = 61这意味着条形图上的平均值要大得多。关于如何将int更改为时间的任何想法?

SELECT  FastestSegmentTime , SlowestSegmentTime,  
 (SELECT AVG(DateDiff(SECOND, tblTrace.trStart , tblTrace.trFinish)) 
    FROM tblTrace
   INNER JOIN 
         tblUsers 
      ON usrID = tr_usrID
   WHERE tblTrace.trFinish IS NOT NULL 
     AND tblTrace.trObjectType LIKE 'Segment%' 
     AND tblTrace.tr_vnuID = @vnuID 
     AND tblTrace.trStart BETWEEN @StartDate AND @EndDate 
     AND tblUsers.usrEmail NOT LIKE '%@test%' ) as AvgSegmentTime
    FROM ( 
      SELECT MIN(CAST(tblTrace.trFinish - tblTrace.trStart AS TIME(2))) AS FastestSegmentTime,  
             MAX(CAST(tblTrace.trFinish - tblTrace.trStart AS TIME(2))) AS SlowestSegmentTime            
        FROM tblTrace 
       INNER JOIN 
             tblUsers 
          ON usrID = tr_usrID
       WHERE tr_vnuID = @vnuID 
         AND trFinish IS NOT NULL 
         AND tr_usrID IS NOT NULL 
         AND trObjectType LIKE 'Segment%'  
         AND trStart BETWEEN @StartDate AND @EndDate 
         AND tblUsers.usrEmail NOT LIKE '%@test%'
) coreData

2 个答案:

答案 0 :(得分:1)

尝试将平均的datediff添加为0,如下所示:

SELECT  FastestSegmentTime , SlowestSegmentTime,  
 (SELECT dateadd(second, AVG(DateDiff(SECOND, tblTrace.trStart , tblTrace.trFinish)),0) as AVGSegmentTime
    FROM tblTrace
   INNER JOIN 
         tblUsers 
      ON usrID = tr_usrID
   WHERE tblTrace.trFinish IS NOT NULL 
     AND tblTrace.trObjectType LIKE 'Segment%' 
     AND tblTrace.tr_vnuID = @vnuID 
     AND tblTrace.trStart BETWEEN @StartDate AND @EndDate 
     AND tblUsers.usrEmail NOT LIKE '%@test%' ) as AvgSegmentTime
    FROM ( 
      SELECT MIN(CAST(tblTrace.trFinish - tblTrace.trStart AS TIME(2))) AS FastestSegmentTime,  
             MAX(CAST(tblTrace.trFinish - tblTrace.trStart AS TIME(2))) AS SlowestSegmentTime            
        FROM tblTrace 
       INNER JOIN 
             tblUsers 
          ON usrID = tr_usrID
       WHERE tr_vnuID = @vnuID 
         AND trFinish IS NOT NULL 
         AND tr_usrID IS NOT NULL 
         AND trObjectType LIKE 'Segment%'  
         AND trStart BETWEEN @StartDate AND @EndDate 
         AND tblUsers.usrEmail NOT LIKE '%@test%'
) coreData

答案 1 :(得分:1)

就我个人而言,我首先使用分组来提高效率(当然这部分是可选的)但是你的问题正在发生,因为它是平均秒数而是将其他时间报告为时间。使用以下answer,您可以在秒和时间之间进行转换,从而可以相应地进行比较,或者您可以将以前的列转换为秒。

SELECT  
    FastestSegmentTime = MIN(CAST(tblTrace.trFinish - tblTrace.trStart AS TIME(2)))
    , SlowestSegmentTime = MAX(CAST(tblTrace.trFinish - tblTrace.trStart AS TIME(2)))
    , AvgSegmentTime = DATEADD(ms, 1000 * AVG(DATEDIFF(SECOND, tblTrace.trStart , tblTrace.trFinish)),0)
FROM 
    tblTrace
    INNER JOIN tblUsers ON usrID = tr_usrID
WHERE 
    (tblTrace.trFinish IS NOT NULL)
    AND (tblTrace.trObjectType LIKE 'Segment%') 
    AND (tblTrace.tr_vnuID = @vnuID) 
    AND (tblTrace.trStart BETWEEN @StartDate AND @EndDate)
    AND (tblUsers.usrEmail NOT LIKE '%@test%')