SQLServer 根据时间查询A表的人员和记录点数据 只取最新的一条 有人知道么

给大家模拟的表和数据

create table kaoqin ( userid varchar(4) not null, holeid varchar(2) not null, recodetime nvarchar(20) not null )
insert into kaoqin values('0001','05','2013-08-22 13:52:21')
insert into kaoqin values('0002','01','2013-08-22 13:52:18')
insert into kaoqin values('0002','06','2013-08-22 13:52:21')
insert into kaoqin values('0003','08','2013-08-22 13:52:35')
最后的结果我是要一次性 查询出 '0001','05','2013-08-22 13:52:21' | '0002','06','2013-08-22 13:52:21' |'0003','08','2013-08-22 13:52:35'

第1个回答  2013-08-24
这个非常简单
首先你是要查询各个不同的,就需要用到group by .
然后是取最后的记录时间点,你就需要用到 max()函数
select userid as 卡号,holeid as 记录点,max(recodetime) as 记录时间 from kaoqin
group by userid,holeid

就这样就行了,其实SQL把需求分开来看很好写的。望楼主采纳!
第2个回答  推荐于2018-05-10
select * from table A where exists (select * from (select 卡号,记录点,max(记录时间) 记录时间 from table) B where A.卡号 = B.卡号 and A.记录点 = B.记录点 and A.记录时间 = B.记录时间  )

本回答被网友采纳
第3个回答  2013-08-22
select 卡号,记录点 ,max(记录时间) as 记录时间
from table group by 卡号,记录点,记录时间

追问

还是不对 兄弟

追答select 卡号,记录点 ,max(记录时间) as 记录时间
from table group by 卡号,记录点

相似回答