您的查询只返回关闭位置的人...
要返回谁打开和谁关闭,您需要将 where 子句更改为
where rk2 = 1 or rk1 = 1
如果您将同时从多个 sDept 获得订单,则应像这样为 sDept 分区订单
RANK() OVER ( partition by sDept ORDER BY dtTimeIn) rk1, --earliest record gets 1
RANK() OVER ( partition by sDept ORDER BY dtTimeOut DESC) rk2 --latest record get
看...
设置环境以运行您的查询
declare @TimeClock table ( lEmployeeID int, dtTimeIn datetime, dtTimeOut datetime)
declare @Employees table ( lEmployeeID int, sfirstname varchar(max),sDept varchar(max))
declare @getDate date
set @getDate ='02/12/2013'
insert @Employees
values (1,'Ana','1')
,(2,'Pedro','1')
,(3,'Alfred','2')
insert @TimeClock
values (1 ,'02/12/2013 08:30','02/11/2013 11:30')
,(2 ,'02/12/2013 08:00','02/11/2013 11:00')
,(3 ,'02/12/2013 08:15','02/11/2013 11:15')
您的查询...
SELECT * FROM (
SELECT timeclock.dtTimeIn, timeclock.dtTimeOut, employees.sfirstname,
RANK() OVER (partition by sDept ORDER BY dtTimeIn) rk1, --earliest record gets 1
RANK() OVER (partition by sDept ORDER BY dtTimeOut DESC) rk2 --latest record gets 1
FROM @TimeClock timeclock
INNER JOIN @Employees Employees
ON TimeClock.lEmployeeID = Employees.lEmployeeID
WHERE (dtTimeIn > dateadd(day, datediff(day, 0, @getDate)-1, 0)) AND (dtTimeOut < dateadd(day, datediff(day, 0, @getDate), 0)) AND
(sDept IN ('1', '2', '3'))
) A
WHERE rk2=1
返回...
dtTimeIn dtTimeOut sfirstname rk1 rk2
2013-02-12 08:30:00.000 2013-02-11 11:30:00.000 Ana 3 1
看.. 刚刚关闭的人在结果集中
更改 where 子句...
SELECT * FROM (
SELECT timeclock.dtTimeIn, timeclock.dtTimeOut, employees.sfirstname,
RANK() OVER ( ORDER BY dtTimeIn) rk1, --earliest record gets 1
RANK() OVER (ORDER BY dtTimeOut DESC) rk2 --latest record gets 1
FROM @TimeClock timeclock
INNER JOIN @Employees Employees
ON TimeClock.lEmployeeID = Employees.lEmployeeID
WHERE (dtTimeIn > dateadd(day, datediff(day, 0, @getDate)-1, 0)) AND (dtTimeOut < dateadd(day, datediff(day, 0, @getDate), 0)) AND
(sDept IN ('1', '2', '3'))
) A
WHERE rk2=1
or rk1 = 1
同时返回..谁打开谁关闭...
dtTimeIn dtTimeOut sfirstname rk1 rk2
2013-02-12 08:30:00.000 2013-02-11 11:30:00.000 Ana 3 1
2013-02-12 08:00:00.000 2013-02-11 11:00:00.000 Pedro 1 3
2013-02-12 08:15:00.000 2013-02-11 11:15:00.000 Alfred 1 1
Pedro 确实打开了,Ana 关闭了 sDept '1',Alfred 确实打开并关闭了 sDept '2'