2 回答
TA贡献1848条经验 获得超6个赞
这个问题可以通过写sql解决,我给你提供一种sql2005下的一种写法。 假定你有一张表TA,有三个字段ID,AddTime,OriginName [code] if object_id('ta','U') is not null drop table ta; go create table ta( id int identity(1,1) not null , OriginName nvarchar(100) not null, AddTime datetime not null ) go insert into ta values('A','2008-05-01 10:10:11'); insert into ta values('B','2008-05-01 20:10:11'); insert into ta values('C','2008-05-02 09:10:11'); insert into ta values('D','2008-05-02 15:10:11'); insert into ta values('E','2008-05-03 11:10:11'); Go with ta_temp as( select id, originName,AddTime, datePartOfAddTime = cast(year(AddTime) as char(4)) + '-' + cast(month(AddTime) as char(2)) + '-' + cast(day(AddTime) as char(2)) FROM ta ), ta_temp_rn as( SELECT id,originName,AddTime,datePartOfAddTime, rn = ROW_NUMBER() OVER (PARTITION BY datePartOfAddTime ORDER BY AddTime DESC) FROM ta_temp ) SELECT id,originName,AddTime FROM ta_temp_rn WHERE rn = 1 [/code] 以上脚本已测试通过,取每天的第一条,或者最后一条,只需修改 [code] ROW_NUMBER() OVER (PARTITION BY datePartOfAddTime ORDER BY AddTime DESC) [/code] 中的ORDER BY AddTime DESC 或者ASC即可。 sql 2000中写法如下: [code] select ta.* from ta inner join ( select maxAddTime = max(AddTime), datePartOfAddTime = cast(year(AddTime) as char(4)) + '-' + cast(month(AddTime) as char(2)) + '-' + cast(day(AddTime) as char(2)) FROM ta group by cast(year(AddTime) as char(4)) + '-' + cast(month(AddTime) as char(2)) + '-' + cast(day(AddTime) as char(2)) ) temp on ta.addtime = temp.maxAddTime [/code] 2000中写法的效率肯定要低于2005中的。
- 2 回答
- 0 关注
- 356 浏览
添加回答
举报