日期:2014-05-18  浏览次数:20428 次

请大家帮忙, 高分求助一个复杂的SQL统计语句怎么写呢?
是关于刷卡,   根据刷卡纪录求工时的统计语句:
表格如下:
sitename dept ssno r_date in_time out_time
All 85 00016 2006-06-27 2006-6-27   8:00
All 85 00016 2006-06-27 2006-6-27   8:01
All 85 00016 2006-06-27 2006-6-27   8:18
All 85 00016 2006-06-27 2006-6-27   8:19
All 85 00016 2006-06-27 2006-6-27   10:23
All 85 00016 2006-06-27 2006-6-27   10:31
All 85 00016 2006-06-27 2006-6-27   12:03
All 85 00016 2006-06-27 2006-6-27   12:06
All 85 00016 2006-06-27 2006-6-27   12:44
All 85 00016 2006-06-27 2006-6-27   12:54
All 85 00016 2006-06-27 2006-6-27   12:55
All 85 00016 2006-06-27 2006-6-27   13:44
All 85 00016 2006-06-27 2006-6-27   17:31
All 85 00016 2006-06-27 2006-6-27   17:34
All 85 00016 2006-06-30 2006-6-30   8:33
All 85 00016 2006-06-30 2006-6-30   9:33
All 85 00016 2006-06-30 2006-6-30   10:43
All 85 00016 2006-06-30 2006-6-30   11:54
All 85 00016 2006-06-30 2006-6-30   12:53
All 85 00016 2006-06-30 2006-6-30   15:03
All 85 00016 2006-06-30 2006-6-30   16:58
All 85 00016 2006-06-30 2006-6-30   17:25
All 85 00016 2006-07-07 2006-7-7   8:13
All 85 00016 2006-07-07 2006-7-7   10:33
All 85 00016 2006-07-07 2006-7-7   11:51
All 85 00016 2006-07-07 2006-7-7   13:27
All 85 00016 2006-07-07 2006-7-7   13:27
All 85 00016 2006-07-07 2006-7-7   13:28
All 85 00016 2006-07-07 2006-7-7   16:31
All 85 00016 2006-07-07 2006-7-7   16:59
All 85 00016 2006-07-07 2006-7-7   17:17

现在想统计对于同一天的,同一场区内的员工的刷卡纪录,
首先去除,只有刷进入和只有刷出去的纪录。
对于有连续时间刷进入的纪录,取最小的进入时间,  
对于有连续时间刷出去的纪录,取最大的出去时间,   取出这个样子的时间后,把数据合并到一行里面去
而并不是要取一天之内的最小的进入时间和最大的出去时间
这个要得到这个样子的结果:
sitename dept ssno r_date in_time out_time
All 85 00016 2006-06-27   2006-6-27   8:00 2006-6-27   10:31
All 85 00016 2006-06-27   2006-6-27   12:03 2006-6-27   12:44
All 85 00016 2006-06-27   2006-6-27   12:54 2006-6-27   17:34
All 85 00016 2006-06-30   2006-6-30   8:33 2006-6-30   11:54
All 85 00016 2006-06-30   2006-6-30   12:53 2006-6-30   17:25
All 85 00016 2006-07-07   2006-7-7   8:13 2006-7-7   16:31
All 85 00016 2006-07-07   2006-7-7   16:59 2006-7-7   17:17

语句应该怎么写好呢   ?
时间很紧急,
请大家帮我想想办法好吗?   谢谢


------解决方案--------------------
由于你的数据sitename dept ssno三字段全部相同,我测试作了简化,只使用了ssno一个字段,而且只取了6-27和6-30数据测试

--测试数据
declare @t table (
ssno varchar(10),
r_date datetime,
in_time datetime,
out_time datetime
)
insert @t select
'00016 ', '2006-06-27 ', '2006-6-27 8:00 ',null
union all select
'00016 ', '2006-06-27 ',null, '2006-6-27 8:01 '
union all select
'00016 ', '2006-06-27 ',null, '2006-6-27 8:18 '
union all select
'00016 ', '2006-06-27 ',null, '2006-6-27 8:19 '
union all select
'00016 ', '2006-06-27 ',null, '2006-6-27 10:23 '
union all select
'00016 ', '2006-06-27 ',null, '2006-6-27 10:31 '
union all select
'00016 ', '2006-06-27 ', '2006-6-27 12:03 ',null
union all select
'00016 ', '2006-06-27 ', '2006-6-27 12:06 ',null
union all select
'00016 ', '2006-06-27