2013-11-27 182 views
0

我有3次將其放到條形圖上,但無法解決如何將AvgUserSegmentTime更改爲時間格式(00:00:00.00)。現在我得到Fastest = 00:00:00.01 Slowest = 00:00:24:22.22和Avg = 61這意味着條形圖上的平均值要大得多。有關如何將int更改爲一次的想法?從DateDiff將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 

回答

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%') 
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

您可以使用'Cast('00.00:00.00'作爲Time)'而不是'0'作爲'DateAdd'的第三個參數 – bummi