两个日期时间列之间的平均值

时间:2016-10-30 07:49:36

标签: sql-server tsql sql-server-2016

我有以下查询,我试图返回两个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' )  

2 个答案:

答案 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

答案 1 :(得分: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;