from reports t1
) a
where a.group_id<=3
order by a.group_id desc;
这个是取raid分组后,每组前三条记录。
现在要改成,加一个时间参数,取小于这个时间参数的每组前三条记录。求大神帮忙!
5 个解决方案
#1
where a.group_id<=3 and datcol<'2014-01-16 16:12'
#2
这个是在筛选出来的结果中,再筛选,我要的是,在原来的数据基础上进行条件筛选。
#3
SELECT * from ttl a
where 3>(select count(*) from ttl where a.raid=raid and rtime<'你的时间' and a.rtime>rtime)
#4
这样?
select t1.*,(select count(*)+1 from reports where raid=t1.raid and rtime > t1.rtime and datcol<'2014-01-16 16:12') as group_id
from reports t1
) a
where a.group_id<=3 and datcol<'2014-01-16 16:12'
order by a.group_id desc;
select t1.*,(select count(*)+1 from reports where raid=t1.raid and rtime > t1.rtime and datcol<'2014-01-16 16:12') as group_id
from reports t1
) a
where a.group_id<=3 and datcol<'2014-01-16 16:12'
order by a.group_id desc;
#5
正解。里面和外面同时加时间条件达到了,版主厉害,其实,我根据结果也弄出来了,但是自认为sql太差,就放弃了。呵呵
#1
where a.group_id<=3 and datcol<'2014-01-16 16:12'
#2
这个是在筛选出来的结果中,再筛选,我要的是,在原来的数据基础上进行条件筛选。
#3
SELECT * from ttl a
where 3>(select count(*) from ttl where a.raid=raid and rtime<'你的时间' and a.rtime>rtime)
#4
这样?
select t1.*,(select count(*)+1 from reports where raid=t1.raid and rtime > t1.rtime and datcol<'2014-01-16 16:12') as group_id
from reports t1
) a
where a.group_id<=3 and datcol<'2014-01-16 16:12'
order by a.group_id desc;
select t1.*,(select count(*)+1 from reports where raid=t1.raid and rtime > t1.rtime and datcol<'2014-01-16 16:12') as group_id
from reports t1
) a
where a.group_id<=3 and datcol<'2014-01-16 16:12'
order by a.group_id desc;
#5
正解。里面和外面同时加时间条件达到了,版主厉害,其实,我根据结果也弄出来了,但是自认为sql太差,就放弃了。呵呵