日期:2014-05-17 浏览次数:20551 次
)select * from tb t1 where not exists(select 1 from tb t2 where t1.姓名=t2.姓名 and convert(varchar(10),t1.时间,120)=convert(varchar(10),t2.时间,120) and t1.时间>t2.时间)
create table Tbl_0321(name varchar(100), op_date datetime, num int)
insert Tbl_0321(name,op_date,num)
select
'a', '2011-01-01 18:30:00', 100 union all select
'a', '2011-01-01 20:00:00', 130 union all select
'a', '2011-01-02 19:00:00', 150 union all select
'a', '2011-01-02 20:00:00', 170 union all select
'b', '2011-01-01 19:00:00', 120 union all select
'b', '2011-01-01 20:00:00', 130 union all select
'b', '2011-01-02 19:00:00', 150 union all select
'b', '2011-01-02 20:00:00', 170
;with temp as(
select t.name
,CONVERT(varchar(10),t.op_date,120)+' 19:00:00' as op_date
,min(ABS(DATEDIFF(MINUTE,CONVERT(varchar(10),t.op_date,120)+' 19:00:00',t.op_date))) as min_date
from Tbl_0321 t
group by t.name,CONVERT(varchar(10),t.op_date,120)
)
select *
,(select num from Tbl_0321 tb
where temp.name=tb.name
and ABS(dated