SQL View Query需要在2个日期之间计算时间间隔

时间:2017-11-12 16:23:22

标签: sql sql-server database sql-server-2008

我在View Query下方。在计算时间间隔时,如果检出的是同一日期,则仅显示时间间隔。例如,如果我在2017年12月12日办理登机手续,那么我必须在同一日期11/12/201结账,以便计算时间间隔。

但是我想要修改它就像我第二天结账(午夜12:00之后)它还应该计算时间间隔。有些人帮我修改查询以获得所需的结果吗?

查询:

ALTER VIEW [dbo].[TimeAttendanceQuery]
AS
SELECT TOP (100)    PERCENT 
    dbo.AxPerson.Name,
    dbo.AxPerson.IdNumber  AS EmployeeID,
    dbo.TimeAttendance.Badge, 
    dbo.AxPerson.Id, 
    MIN(dbo.TimeAttendance.EventTime) AS EntryTime, 

    MAX(dbo.TimeAttendance.EventTime) AS ExitTime,
    CAST(DATEDIFF(second, MIN(dbo.TimeAttendance.EventTime), MAX(dbo.TimeAttendance.EventTime)) / 60 / 60 / 24 AS NVARCHAR(50)) +
    ':' + CAST(DATEDIFF(second, MIN(dbo.TimeAttendance.EventTime), MAX(dbo.TimeAttendance.EventTime)) / 60 / 60 % 24 AS NVARCHAR(50)) + ':' + CAST(DATEDIFF(second, MIN(dbo.TimeAttendance.EventTime),  MAX(dbo.TimeAttendance.EventTime)) / 60 % 60 AS NVARCHAR(50))
    AS TimeInterval, 
    dbo.TimeAttendance.Event, 
    dbo.AxPerson.Type AS ShitType, 
    dbo.AxPerson.ShiftDesc, 
    CONVERT(Varchar,dbo.TimeAttendance.EventTime, 101) AS EventTIME

FROM
    dbo.AxPerson 
INNER JOIN dbo.TimeAttendance ON dbo.AxPerson.Name = dbo.TimeAttendance.Name
GROUP BY  dbo.AxPerson.Name, dbo.AxPerson.IdNumber, dbo.TimeAttendance.Badge, CONVERT(Varchar, dbo.TimeAttendance.EventTime, 101), dbo.         AxPerson.ShiftDesc, dbo.AxPerson.Id, dbo.TimeAttendance.Event,dbo.AxPerson.Type
ORDER BY dbo.AxPerson.Name, EventTime DESC

GO

1 个答案:

答案 0 :(得分:0)

我不确定您发布的算法,但如果您想从两个日期时间获得时间差,则可以将减法转换为时间。这工作不到24小时。如果你还想要天数(我认为这只有不到一年的时间),那么你可以做差异的日期部分。

例如:

DECLARE @starttime datetime = '2017-11-12 010:20:00'
DECLARE @endtime   datetime = '2017-11-13 08:00:00'

SELECT DATEPART(DAY, @endtime - @starttime) - 1 [Days Passed]
      ,CAST(@endtime - @starttime as time(0)) [Time Passed]
       --WHERE the (0) in time(0) is for the milliseconds to return. 

提供输出:

Days Passed Time Passed
0           21:40:00

如果您不关心这些日子,那么您的代码可以像这样修改:

ALTER VIEW [dbo].[TimeAttendanceQuery]
AS
SELECT Name, EmployeeID, Badge, Id, EntryTime,
       CAST(ExitTime - EntryTime as time(0)) [TimeInterval],
       Event, ShiftType, ShiftDesc
      ,CONVERT(Varchar, EventTime, 101) AS EventTIME
  FROM (
         SELECT
           dbo.AxPerson.Name,
           dbo.AxPerson.IdNumber  AS EmployeeID,
           dbo.TimeAttendance.Badge, 
           dbo.AxPerson.Id, 
           MIN(dbo.TimeAttendance.EventTime) AS EntryTime,  
           MAX(dbo.TimeAttendance.EventTime) AS ExitTime,             
           dbo.TimeAttendance.Event, 
           dbo.AxPerson.Type AS ShiftType, 
           dbo.AxPerson.ShiftDesc, 
           dbo.TimeAttendance.EventTime

           FROM dbo.AxPerson INNER JOIN dbo.TimeAttendance 
                                     ON dbo.AxPerson.Name = dbo.TimeAttendance.Name

           GROUP BY  
           dbo.AxPerson.Name, 
           dbo.AxPerson.IdNumber, 
           dbo.TimeAttendance.Badge, 
           dbo.AxPerson.Id, 
           dbo.TimeAttendance.Event,
           dbo.AxPerson.Type
           dbo.AxPerson.ShiftDesc,
           dbo.TimeAttendance.EventTime,
       ) AS dT