您的位置:首页 > 数据库

如何通过SQL语句获取分组中的某几条记录

2010-01-20 09:40 381 查看
如下图表为t_FR

IDFRIDSIDPIDLastChangedDateStartDateEndDate
11222010-10-012009-10-012008-10-01
21112008-10-012009-10-012008-10-01
31222008-12-012009-10-012008-10-01
42432009-10-012009-10-012008-10-01
52432009-10-012009-10-012008-10-01
63642010-10-012009-10-012008-10-01
73642009-11-012009-10-012008-10-01
84642010-10-052009-10-012008-10-01
用sql实现按照FRID,SID及PID分组中LastChangeDate为最新的一条记录。

Select * from t_FR t wher t.ID in (select top 1 ID from t_FR where FRID=t.FRID and SID=t.SID and PID=t.PID order by LastChangedDate desc)

结果为:

IDFRIDSIDPIDLastChangedDateStartDateEndDate
11222010-10-012009-10-012008-10-01
52432009-10-012009-10-012008-10-01
73642009-11-012009-10-012008-10-01
84642010-10-052009-10-012008-10-01
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: