简体   繁体   English

SQL从同一字段获取datediff

[英]SQL getting datediff from same field

I have a problem. 我有个问题。 I need to get the date difference in terms of hours in my table but the problem is it is saved in the same field. 我需要在表中以小时为单位获取日期差,但问题是它被保存在同一字段中。 This is my table would look like. 这是我的桌子的样子。

  RecNo.    Employeeno   recorddate   recordtime    recordval
   1           001        8/22/2014      8:15 AM         1
   2           001        8/22/2014      5:00 PM         2
   3           001        8/24/2014      8:01 AM         1
   4           001        8/24/2014      5:01 PM         2

1 indicates time in and 2 indicates time out. 1表示超时,2表示超时。 Now, How will i get the number of hours worked for each day? 现在,我如何获得每天的工作时间? What i want to get is something like this. 我想要得到的是这样的东西。

Date       hoursworked
8/22/2014       8
8/24/2014       8

I am using VS 2010 and SQL server 2005 我正在使用VS 2010和SQL Server 2005

You could self-join each "in" record with its corresponding "out" record and use datediff to subtract them: 您可以将每个“输入”记录与其对应的“输出”记录进行自我datediff ,并使用datediff减去它们:

SELECT     time_in.employeeno AS "Employee No",
           time_in.recorddate AS "Date",
           DATEDIFF (hour, time_in.recordtime, time_out.recordtime) 
            AS "Hours Worked"
FROM       (SELECT *
            FROM   my_table
            WHERE  recordval = 1) time_in
INNER JOIN (SELECT *
            FROM   my_table
            WHERE  recordval = 2) time_out 
           ON time_in.employeeno = time_out.employeeno AND
              time_in.recorddate = time_out.recorddate

If you always record time in and time out for every employee, and just one per day, using a self-join should work: 如果您始终记录每位员工的进出超时时间,并且每天仅记录一次,那么使用自加入应该有效:

SELECT 
    t1.Employeeno, 
    t1.recorddate, 
    t1.recordtime AS [TimeIn], 
    t2.recordtime AS [TimeOut], 
    DATEDIFF(HOUR,t1.recordtime, t2.recordtime) AS [HoursWorked]
FROM Table1 t1
INNER JOIN Table1 t2 ON 
    t1.Employeeno = t2.Employeeno 
    AND t1.recorddate = t2.recorddate 
WHERE t1.recordval = 1 AND t2.recordval = 2

I included the recordtime fields as time in, time out, if you don't want them just remove them. 如果您不希望将它们删除,那么我将recordtime字段包括为超时,超时。

Note that this datediff calculation gives 9 hours, and not 8 as you suggested. 请注意,此datediff计算得出9小时,而不是您建议的8小时。

Sample SQL Fiddle 示例SQL提琴

Using this sample data: 使用此样本数据:

with table1 as (
    select * from ( values
         (1,'001', cast('20140822' as datetime),cast('08:15:00 am' as time),1)
        ,(2,'001', cast('20140822' as datetime),cast('05:00:00 pm' as time),2)

        ,(3,'001', cast('20140824' as datetime),cast('08:01:00 am' as time),1)
        ,(4,'001', cast('20140824' as datetime),cast('04:59:00 pm' as time),2)

        ,(5,'001', cast('20140825' as datetime),cast('10:01:00 pm' as time),1)
        ,(6,'001', cast('20140826' as datetime),cast('05:59:00 am' as time),2)
    )data(RecNo,EmployeeNo,recordDate,recordTime,recordVal)
)

this query 这个查询

SELECT
     Employeeno
    ,convert(char(10),recorddate,120) as DateStart
    ,convert(char(5),cast(TimeIn as time))  as TimeIn
    ,convert(char(5),cast(TimeOut as time)) as TimeOut
    ,DATEDIFF(minute,timeIn, timeOut) / 60 AS [HoursWorked]
    ,DATEDIFF(minute,timeIn, timeOut) % 60 AS [MinutesWorked]
FROM (
    SELECT 
        tIn.Employeeno, 
        tIn.recorddate, 
        dateadd(minute, datediff(minute,0,tIn.recordTime), tIn.recordDate)
             as TimeIn,             
        ( SELECT TOP 1 
             dateadd(minute, datediff(minute,0,tOut.recordTime), tOut.recordDate)
             as TimeOut
          FROM Table1 tOut
          WHERE tOut.RecordVal = 2
            AND tOut.EmployeeNo = tIn.EmployeeNo
            AND tOut.RecNo > tIn.RecNo
          ORDER BY tOut.EmployeeNo, tOut.RecNo
        ) as TimeOut
    FROM Table1 tIn
    WHERE tIn.recordval = 1
) T

yields (as desired) 产量(根据需要)

Employeeno DateStart  TimeIn TimeOut HoursWorked MinutesWorked
---------- ---------- ------ ------- ----------- -------------
001        2014-08-22 08:15  17:00   8           45
001        2014-08-24 08:01  16:59   8           58
001        2014-08-25 22:01  05:59   7           58

No assumptions are made about shifts not running across midnight (see case 3). 没有关于没有在午夜进行的轮班的假设(见案例3)。

This particular implementation may not be the most performant way to construct this correlated subquery, so if there is a performance problem come back and we can look at it again. 这种特定的实现可能不是构造此相关子查询的最高效的方法,因此,如果出现性能问题,请再次查看。 However running those tests requires a large dataset which I don't feel like constructing just now. 但是,运行这些测试需要庞大的数据集,而我现在还不想构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM