Sql 徽章刷卡团体记录

Sql 徽章刷卡团体记录,sql,sql-server,group-by,Sql,Sql Server,Group By,我尝试了自连接,并尝试读取下一条记录并比较machinename和value..但是SQL db没有id号,然后根据我的排序方式对数据进行了不一致的分组..有人可以提供一些帮助吗。我现在手头没有SQL datetime machinename value 02/02/2015 07:12:33 machine1 11111 02/02/2015 07:22:33 machine1 11111 02/02/2015 08:09:12 machine8 222

我尝试了自连接,并尝试读取下一条记录并比较machinename和value..但是SQL db没有id号,然后根据我的排序方式对数据进行了不一致的分组..有人可以提供一些帮助吗。我现在手头没有SQL

datetime            machinename value
02/02/2015 07:12:33 machine1    11111
02/02/2015 07:22:33 machine1    11111
02/02/2015 08:09:12 machine8    22222
02/02/2015 08:45:35 machine2    22222
02/02/2015 09:12:33 machine8    22222
02/02/2015 09:13:15 machine8    22222
02/02/2015 08:48:33 machine2    11111
02/02/2015 08:48:37 machine2    11111
02/02/2015 08:48:56 machine2    11111
02/02/2015 10:22:12 machine7    33333
02/02/2015 10:55:12 machine2    11111
02/02/2015 12:25:23 machine3    33333
02/02/2015 13:01:05 machine3    33333
02/02/2015 23:46:23 machine3    11111
02/03/2015 01:01:05 machine3    11111
预期结果

> datetime start       datetime end         machinename        value
> 02/02/2015 07:12:33  02/02/2015 07:22:33  machine1           11111
> 02/02/2015 08:09:12  NULL                 machine8           22222
> 02/02/2015 08:45:35  NULL                 machine2           22222
> 02/02/2015 09:12:33  02/02/2015 09:13:15  machine8           22222
> 02/02/2015 08:48:33  02/02/2015 08:48:37  machine2           11111
> 02/02/2015 08:48:56  NULL                 machine2           11111
> 02/02/2015 10:22:12  NULL                 machine7           33333
> 02/02/2015 10:55:12  NULL                 machine2           11111
> 02/02/2015 12:25:23  02/02/2015 13:01:05  machine3           33333
> 02/02/2015 23:46:23  02/03/2015 01:01:05  machine3           11111
一个人可以在几秒钟内刷入/刷出多个时间,如下所示:

02/02/2015 08:48:33  02/02/2015 08:48:37  machine2           11111
02/02/2015 08:48:56  NULL                 machine2           11111
或者,如果他们离开一台机器,去另一台机器上刷卡,就要刷卡一次。 例如:


这就是你想要的吗

select 
  stamp as starttime,
  (select min(stamp)
   from table1 as t1a
   where t1a.machinename = t1.machinename
   and t1a.id = t1.id
   and t1a.stamp > t1.stamp
   and t1a.stamp < dateadd(mi,15,t1.stamp)
   ) as endtime,
  machinename, 
  id
from table1 as t1
where stamp <>  (
  select max(stamp)  
  from table1 as t1b
  where t1b.machinename = t1.machinename
  and t1b.id = t1.id
  and t1b.stamp > dateadd(mi,-15,t1.stamp)
  )
在这把小提琴里:

结果:

starttime                     endtime                       machinename    id
February, 02 2015 07:12:33    February, 02 2015 07:22:33    machine1    11111
February, 02 2015 08:09:12    (null)                        machine8    22222
February, 02 2015 08:45:35    (null)                        machine2    22222
February, 02 2015 09:12:33    February, 02 2015 09:13:15    machine8    22222
February, 02 2015 08:48:33    February, 02 2015 08:48:37    machine2    11111
February, 02 2015 10:22:12    (null)                        machine7    33333
February, 02 2015 10:55:12    (null)                        machine2    11111
February, 02 2015 12:25:23    February, 02 2015 13:01:05    machine3    33333
February, 02 2015 23:46:23    February, 03 2015 01:01:05    machine3    11111

你在使用什么数据库?oracle、sql server、mysql等等,我不确定我是否遵循了这些标准。如何定义开始日期与结束时间?是11111222233333列吗?我觉得会有一些窗口功能,但是的,知道您使用的是什么数据库服务器将是一个良好的开端!我正在使用SQL server。列名是11111222233333的值。您使用的SQL Server版本是什么?SQL Server 2012+具有超前和滞后功能,可帮助处理此类查询,即SQL Server 2012。。我尝试了lead&lag。Keep get'lead'不是公认的内置函数名。我已经安装了分析插件。我会在早上访问数据库时尝试一下。谢谢检查小提琴,看看它是否能满足您的需要。运行select stamp as starttime,从表1中选择minstamp作为t1a,其中t1a.machinename=t1.machinename和t1a.id=t1.id和t1a.stamp>t1.stamp和t1a.stampwith cte as ( select stamp as starttime, (select stamp from table1 as t1a where t1a.machinename = t1.machinename and t1a.id = t1.id and stamp = (select min(stamp) from table1 as t1aa where t1aa.stamp > t1.stamp )) as endtime, machinename, id from table1 as t1 ) select * from cte where starttime not in (select endtime from cte where endtime is not null)
starttime                     endtime                       machinename    id
February, 02 2015 07:12:33    February, 02 2015 07:22:33    machine1    11111
February, 02 2015 08:09:12    (null)                        machine8    22222
February, 02 2015 08:45:35    (null)                        machine2    22222
February, 02 2015 09:12:33    February, 02 2015 09:13:15    machine8    22222
February, 02 2015 08:48:33    February, 02 2015 08:48:37    machine2    11111
February, 02 2015 10:22:12    (null)                        machine7    33333
February, 02 2015 10:55:12    (null)                        machine2    11111
February, 02 2015 12:25:23    February, 02 2015 13:01:05    machine3    33333
February, 02 2015 23:46:23    February, 03 2015 01:01:05    machine3    11111