如何选择
000001 09/02/2013 7:58 In
000002 09/02/2013 7:55 In
000003 09/02/2013 7:48 In
000004 09/02/2013 8:00 In
000005 09/02/2013 8:00 In
000006 09/02/2013 8:00 In
000007 09/02/2013 8:05 In
000008 09/02/2013 8:06 In
000009 09/02/2013 8:06 In
000010 09/02/2013 8:07 In
000001 09/03/2013 01:00 Out
000002 09/02/2013 16:00 Out
000003 09/02/2013 16:01 Out
000004 09/02/2013 16:02 Out
000005 09/02/2013 16:03 Out
000006 09/02/2013 16:04 Out
000007 09/02/2013 16:30 Out
000008 09/02/2013 16:05 Out
000009 09/02/2013 16:10 Out
000010 09/02/2013 17:30 Out
000001 09/03/2013 7:58 In
000002 09/03/2013 7:55 In
000003 09/03/2013 7:48 In
000004 09/03/2013 8:00 In
000005 09/03/2013 8:00 In
000006 09/03/2013 8:00 In
000007 09/03/2013 8:05 In
000008 09/03/2013 8:06 In
000009 09/03/2013 8:06 In
000010 09/03/2013 8:07 In
000001 09/03/2013 16:00 Out
000002 09/03/2013 16:00 Out
000003 09/03/2013 16:01 Out
000004 09/03/2013 16:02 Out
000005 09/03/2013 16:03 Out
000006 09/03/2013 16:04 Out
000007 09/03/2013 16:30 Out
000008 09/03/2013 16:05 Out
000009 09/03/2013 16:10 Out
000010 09/03/2013 17:30 Out
输出将类似于
Employee No. | Login Date | LogOut Date
000001 | 09/02/2013 7:58 | 09/03/2013 01:00
000003 | 09/02/2013 7:55 | 09/02/2013 16:00
答案 0 :(得分:1)
你在寻找这样的东西:
create table #events (Employee varchar(20),EventTime datetime,EventType varchar(10));
insert #events select '000001','09/02/2013 7:58','In';
insert #events select '000002','09/02/2013 7:55','In';
insert #events select '000003','09/02/2013 7:48','In';
insert #events select '000001','09/03/2013 01:00','Out';
insert #events select '000002','09/02/2013 16:00','Out';
insert #events select '000003','09/02/2013 16:01','Out';
insert #events select '000001','09/03/2013 7:58','In';
insert #events select '000002','09/03/2013 7:55','In';
insert #events select '000003','09/03/2013 7:48','In';
insert #events select '000001','09/03/2013 16:00','Out';
insert #events select '000002','09/03/2013 16:00','Out';
insert #events select '000003','09/03/2013 16:01','Out';
select Employee,LoginTime=EventTime,LogoutTime
from #events ein
cross apply (select top(1) LogoutTime=EventTime from #events eout where eout.Employee=ein.Employee and eout.EventTime>ein.EventTime and eout.EventType='Out' order by eout.EventTime) eout
where ein.EventType='In';
答案 1 :(得分:0)
您可以使用PIVOT函数来获取结果,但首先我会提供一个序列或不同的值,您可以使用它来配置登录和注销日期。这可以通过在查询中添加row_number()
来完成,这样做可以让您为每个员工返回多行。
数据透视的代码类似于以下内容:
select employeeno,
loginDate = [In],
logoutDate = [Out]
from
(
select employeeno,
date,
inout,
row_number() over(partition by employeeno, inout
order by date) seq
from yourtable
) d
pivot
(
max(date)
for inout in ([In], [Out])
) piv
order by employeeno;
见SQL Fiddle with Demo。此查询将返回类似于:
的结果| EMPLOYEENO | LOGINDATE | LOGOUTDATE |
| 1 | 2013-09-02 07:58:00 | 2013-09-03 01:00:00 |
| 1 | 2013-09-03 07:58:00 | 2013-09-03 16:00:00 |
| 2 | 2013-09-02 07:55:00 | 2013-09-02 16:00:00 |
| 2 | 2013-09-03 07:55:00 | 2013-09-03 16:00:00 |
| 3 | 2013-09-02 07:48:00 | 2013-09-02 16:01:00 |
| 3 | 2013-09-03 07:48:00 | 2013-09-03 16:01:00 |
| 4 | 2013-09-02 08:00:00 | 2013-09-02 16:02:00 |
| 4 | 2013-09-03 08:00:00 | 2013-09-03 16:02:00 |