我有一个使用c#和SQL Server 2005进行时间和考勤的应用程序。在SQL中,我创建了一个包含3个参数的存储过程。
事件表是这样的:
UnitTS RdrName RdrIn_Out CrdName
201401030759 Office1 B Mike
201401030819 Office2 B Alex
201401030944 Office2 B Alex
201401030947 Office2 B Tomy
201401030955 Office3 B John
201401030955 Office3 A John
201401031015 Office1 B Alex
201401031034 Office1 A Alex
201401031457 Office1 B Alex
201401031626 Office1 A Alex
201401031649 Office2 B Janina
201401031727 Office2 B Janina
执行存储过程后,我得到以下结果:
CrdName|RdrName | Date | In_Time | Out_Time | Duration
Alex Office 1 03-01-2014 09:35 09:41 360
Alex Office 1 03-01-2014 unknown 09:41 0
Alex Office 1 03-01-2014 14:58 14:59 60
Alex Office 1 03-01-2014 unknown 14:59 0
这是查询
@Emp nvarchar(50),
@Start_Date nvarchar(50),
@End_Date nvarchar(50)
as
with Ordered as
( select
convert(varchar(15), cast(Substring(unitts,1,8) as DATE), 105 ) as Data,
Substring(UnitTS,9,2)+':'+ Substring(UnitTS,11,2) as EventTime,
case when RdrHead = 'A' THEN 'OUT' ELSE 'IN 'END as Reader,
[RdrName],
[CrdName],
rn = row_number() over (order by RdrName)
from
Time.dbo.History
where
(UnitNr = '3' and RdrNr IN ('0','2','3') OR UnitNr = '4' and RdrNr IN('1','6'))
and Type = 'A'
and Substring(unitts, 1, 8) >= @Start_Date
and Substring(unitts, 1, 8) <= @End_Date and (CrdName in (@emp) or @emp= 'all')
)
select
o_out.CrdName,
o_out.RdrName,
o_out.Data,
case when o_in.EventTime is null then 'unknown' else o_in.EventTime end In_Time,
[Out_Time] = o_out.EventTime ,
case when cast(datediff (s,o_in.EventTime ,o_out.EventTime) AS int) is null then '0' ELSE cast(datediff (S,o_in.EventTime ,o_out.EventTime) as int) END Duration
from Ordered o_out
left join Ordered o_in
on o_in.rn = o_out.rn - 1
and o_in.Reader ='in'
where o_out.Reader= 'out'
如果我想选择一个CrdName
,代码就可以了。问题是当我想要选择所有CrdName
时。结果不正确。我尝试按CrdName
订购数据,但没有结果。
我想,我需要通过UnitTS,RdrName和CrdName以某种方式订购Events表
有什么解决方案吗?谢谢