如何使用SQL查询此数据?

时间:2011-11-29 05:22:49

标签: c# .net sql

我正在研究考勤管理系统。

我的表数据是这样的:

LogId      EmployeeId      LogTime                          Date
31         22               09:44:00 AM                    2011-11-04 00:00:00.000
40         22               01:51:00 PM                    2011-11-04 00:00:00.000
43         22               02:14:00 PM                    2011-11-04 00:00:00.000 
45         22               07:36:00 PM                    2011-11-04 00:00:00.000

我希望将第一个LogTime(09:44:00 AM)显示为INTime,将第二个LogTime(01:51:00 PM)显示为OutTime,将第三个LogTime(02:14:00 PM)显示为InTime Etc ..然后我想要了解特定日期所有INOut时间的摘要。

3 个答案:

答案 0 :(得分:0)

SELECT ROW_NUMBER() OVER (PARTITION BY EmployeeId ORDER BY LogId) % 2 AS InTime,
       * FROM TimeStampTable

对于每个InTime = 1EmployeeId,其余为InTime = 0提供时间1,3,5 ... ...

答案 1 :(得分:0)

我的解决方案:

select *, Case (ROW_NUMBER() over (ORDER BY LogId)) % 2 
When 1 Then 'In Time' Else 'Out Time' End as 'Row' from @Table

希望这是你真正需要的。

select CASE(N.Row) 
When 1 then N.LogTime Else null End as InTime, 
CASE(N.Row) When 0 then N.LogTime Else null End as OutTime, N.* from 
( select ROW_NUMBER() over (ORDER BY LogId) % 2 as 'Row',* from @Table ) N 

答案 2 :(得分:-1)

我试图通过将问题分成更小的问题来做到这一点  SQL Server中可用的合并函数可以用于我创建了一个名为timing with fields的表(logid,Employeeid,LogTime,Date)

打孔可以使用以下查询收集记录

declare @Punch nvarchar(100)
set @punch=''

select @punch=Coalesce(cast(datepart(HH,logtime )as nvarchar)+':'+cast(datepart(MI,logtime )as nvarchar)+(case (row_number() over (order by logid))%2 when 0 then ' Out' else ' In' end)+','+@punch,@punch) from timing order by logid desc

select @punch as punches 

并且在此期间你可以使用这样的东西 -

declare @duration int
set @duration=0
SELECT
   @duration = coalesce((case row_number() over (order by t1.logid) %2 
   when 1 
   then cast(datediff(mi,t1.logtime,isnull(t2.logtime,t1.logtime))as int) else 0 end)+@duration,@duration)
FROM
    timing AS t1
LEFT JOIN
   timing AS t2
      ON t2.logid = (SELECT MIN(logid) FROM timing WHERE logid > t1.logid)
select @duration