一条sql语句求救,间隔取数据
表中有一列为时间列
2011-08-03 19:25:27
2011-08-03 19:25:32
2011-08-03 19:25:37
2011-08-03 19:25:43
2011-08-03 19:25:48
2011-08-03 19:25:54
2011-08-03 19:25:59
2011-08-03 19:26:04
2011-08-03 19:26:10
2011-08-03 19:26:15
2011-08-03 19:26:20
2011-08-03 19:26:26
2011-08-03 19:26:31
2011-08-03 19:26:37
2011-08-03 19:26:43
2011-08-03 19:26:48
2011-08-03 19:26:53
2011-08-03 19:26:59
2011-08-03 19:27:04
2011-08-03 19:27:09
2011-08-03 19:27:15
现在想从这个表中取数据,不想显示这么多记录,只想显示每分钟的第一条记录就行。
比如上面的记录 只想显示为
2011-08-03 19:25:27
2011-08-03 19:26:04
2011-08-03 19:27:04
[解决办法]
select * from tb t where col=(select max(col1) from tb where convert(varchar(16),col,120)=convert(varchar(16),t.col,120))
[解决办法]
select * from tb awhere not exists (select 1 from tb b where datediff(minute, a.dt, b.dt) = 0 and b.dt < a.dt)
[解决办法]
select a1.* from(
select row_number() over(partition by convert(varchar(100), datetimeObj, 23)+' '+convert(varchar(16),datepart(hh,datetimeObj))+':'+convert(varchar(16),datepart(mi,datetimeObj)) order by datetimeObj asc) as rowindex,* from table1) a1 where rowindex=1
注释:datetimeObj 表内时间字段名 table1 表名
[解决办法]
with cte as ( select '2011-08-03 19:25:27' as v_time union all select '2011-08-03 19:25:32' union all select '2011-08-03 19:25:37' union all select '2011-08-03 19:25:43' union all select '2011-08-03 19:25:48' union all select '2011-08-03 19:25:54' union all select '2011-08-03 19:25:59' union all select '2011-08-03 19:26:04' union all select '2011-08-03 19:26:10' union all select '2011-08-03 19:26:15' union all select '2011-08-03 19:26:20' union all select '2011-08-03 19:26:26' union all select '2011-08-03 19:26:31' union all select '2011-08-03 19:26:37' union all select '2011-08-03 19:26:43' union all select '2011-08-03 19:26:48' union all select '2011-08-03 19:26:53' union all select '2011-08-03 19:26:59' union all select '2011-08-03 19:27:04' union all select '2011-08-03 19:27:09' union all select '2011-08-03 19:27:15' ) , cte2 as ( select v_time, ROW_NUMBER() over(partition by convert(varchar(16),v_time,120) order by v_time desc) as v_rk from cte ) select * from cte2 where v_rk='1'