一张表T1,字段如下
(
id decimal(18,0),
name varchar(10),
recordTime datetime
)
表中有多条记录,除了最后一个时间字段,其他的都相同如下
1 'zhangsan' 2011-01-01
1 'zhangsan' 2010-09-14
我如何只取时间最靠近当前的一条记录??
------解决方案--------------------
- SQL code
select * from T1 a where not exists(select 1 from T1 where name=a.name and recordTime>a.recordTime)
------解决方案--------------------
row_number()over(partition by order by )
------解决方案--------------------
- SQL code
select * from t1 a where not exists(select 1 from t1 where abs(datediff(mi,recordtime,getdate()))<abs(datediff(mi,a.recordtime,getdate()))
------解决方案--------------------
- SQL code
select top 1 * from t1 order by recordTime desc
------解决方案--------------------
和 当前日期做 差,取差值最小的
------解决方案--------------------
select DATEDIFF(ms,GETDATE(),getdate()+1)---毫秒
select DATEDIFF(day,GETDATE(),getdate()+1)---天
select DATEDIFF(HOUR,GETDATE(),getdate()+1)---小时
select DATEDIFF(Minute,GETDATE(),getdate()+1)---分钟
select DATEDIFF(Second,GETDATE(),getdate()+1)---秒