2016-10-30 137 views
0

我有以下查詢,我試圖返回兩個DateTime列之間的平均值。兩個日期時間列之間的平均值

我的問題是它想讓我組SoftwarePackage1_VersionNumbers.EnvShortName,我不想這樣做。類似於在此查詢的方法之一

SELECT DBO.softwarepackage1_versionnumbers.envshortname                     AS [Environment Name], 
     DBO.softwarepackage1_versionnumbers.currentversionnumber                   AS [SoftwarePackage1 Current Version Number], 
     DBO.softwarepackage1_versionnumbers.currentbuildname                    AS [Current SoftwarePackage1 Build], 
     DBO.softwarepackage1_versionnumbers.currentlastassociatedchangeset                AS [Last Associated Changeset], 
     DBO.softwarepackage1_versionnumbers.deploymentdate                    AS [SoftwarePackage1 Deployment Date], 
     DBO.softwarepackage2_versionnumbers.currentsoftwarepackage2displayedversionnumber             AS [SoftwarePackage2ector Displayed Version Number], 
     DBO.softwarepackage2_versionnumbers.currentsoftwarepackage2fullversionnumber              AS [SoftwarePackage2ector Full Version Number], 
     DBO.softwarepackage2_versionnumbers.currentlastfilewritedatetime                 AS [SoftwarePackage2ector Last File Write Time], 
     DBO.softwarepackage2_versionnumbers.deploymentdate                    AS [SoftwarePackage2ector Deployment Date], 
     Avg(Datediff(MINUTE, DBO.softwarepackage1_versionnumbers.modifieddatetime, DBO.softwarepackage2_versionnumbers.modifieddatetime)) AS [Last Version Check (UTC)] 
FROM DBO.softwarepackage1_versionnumbers 
     FULL OUTER JOIN DBO.softwarepackage2_versionnumbers 
        ON DBO.softwarepackage1_versionnumbers.envid = DBO.softwarepackage2_versionnumbers.envid 
     FULL OUTER JOIN DBO.systemenvironments 
        ON DBO.softwarepackage1_versionnumbers.envid = DBO.systemenvironments.envid 
WHERE (DBO.softwarepackage1_versionnumbers.deleted = '0') 
     AND (DBO.softwarepackage2_versionnumbers.deleted = 0) 
     AND (DBO.systemenvironments.envtype = 'Development') 
+0

平均在什麼範圍?在結果集中的所有行上?在特定的子範圍內? –

+0

這將是該行的平均水平 –

+0

您是否嘗試過我的答案?它有用嗎? –

回答

0

試試:

SELECT name, create_date, modify_date, 
    CONVERT(DATETIME,(CONVERT(FLOAT,create_date)+CONVERT(FLOAT,modify_date))/2) AS AvgDate1, 
    DATEADD(S,DATEDIFF(S,create_date,modify_date)/2.,create_date) AS AvgDate2 
FROM sys.objects 
0

對於一般經過短短兩個日期:

DECLARE @date2 DATETIME = GETDATE(), @date1 DATETIME = DATEADD(DAY,-7,GETDATE()); 

SELECT 
    CASE WHEN @date1<@date2 
     THEN DATEADD(SECOND,DATEDIFF(SECOND,@date1,@date2)/2,@date1) 
     ELSE DATEADD(SECOND,DATEDIFF(SECOND,@date2,@date1)/2,@date2) 
    END; 
相關問題