日期:2014-05-17 浏览次数:20565 次
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([NAME] varchar(4),[TIME] datetime,[STATUS] varchar(2))
insert [tb]
select '张三','2012-09-01 08:28:30','进' union all
select '张三','2012-09-01 08:28:57','进' union all
select '张三','2012-09-01 08:29:02','进' union all
select '张三','2012-09-01 09:28:30','出' union all
select '张三','2012-09-01 09:28:57','出' union all
select '张三','2012-09-01 09:29:02','出'
go
select *
from tb t
where not exists(select 1 from tb where name=t.name and status=t.status and time<t.time)
/**
NAME TIME STATUS
---- ----------------------- ------
张三 2012-09-01 08:28:30.000 进
张三 2012-09-01 09:28:30.000 出
(2 行受影响)
**/
------解决方案--------------------
如果你的表中字段只有这三个,那么使用以下语句就可以
select name,min(time) as time,status
from tb
group by name,status