日期:2014-05-17 浏览次数:20708 次
--try
select * from tb as t
where not exists(select 1 from tb where user=t.user and time>t.time )
and vip !='y'
select * from tb as t
where not exists(select 1 from tb where user=t.user and time>t.time )
and vip !='y'
create table #x(userid varchar(1),riqi datetime,code varchar(3),vip varchar(1))
insert into #x values('a','2011-01-01','11a','')
insert into #x values('a','2011-02-01','11b','')----出来
insert into #x values('a','2011-03-01','11c','y')
insert into #x values('a','2011-04-01','11d','')----出来
insert into #x values('a','2011-05-01','11e','y')
insert into #x values('b','2011-01-01','21a','')----出来
insert into #x values('b','2011-03-01','21b','y')
select c.* from #x c,(
select a.userid,max(a.riqi) riqi from #x a,
(select userid,riqi from #x where vip='y')b
where a.riqi<b.riqi and a.userid=b.userid
group by b.riqi,a.userid
)d
where c.userid=d.userid and c.riqi=d.riqi
--结果:
userid riqi code vip
a 2011-02-01 00:00:00.000 11b
a 2011-04-01 00:00:00.000 11d
b 2011-01-01 00:00:00.000 21a