0

如何选择

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
4

2 回答 2

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';
于 2013-09-12T01:30:39.123 回答
0

您可以使用PIVOT函数来获取结果,但首先我会提供一个序列或不同的值,您可以使用它来配对登录和注销日期。这可以通过row_number()在查询中添加 a 来完成,这样做将允许您为每个员工返回多行。

枢轴的代码将类似于以下内容:

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 |
于 2013-09-12T11:56:29.407 回答