日期:2014-05-18 浏览次数:20601 次
create table tb(会员卡号 int,入会时间 datetime,会员手机 varchar(20))
insert into tb values(1 ,'2011-08-02', '13788888888')
insert into tb values(2 ,'2011-09-03', '13511111111')
insert into tb values(3 ,'2011-10-11', '13777777777')
insert into tb values(4 ,'2011-10-22', '13788888888')
insert into tb values(5 ,'2011-11-02', '13511111111')
insert into tb values(6 ,'2011-12-12', '13511111111')
insert into tb values(7 ,'2012-01-02', '13788888888')
insert into tb values(8 ,'2012-01-07', '13777777777')
insert into tb values(9 ,'2012-02-09', '13788888888')
go
select t.* from tb t where 入会时间 = (select max(入会时间) from tb where 会员手机 = t.会员手机) order by t.会员手机
select t.* from tb t where not exists (select 1 from tb where 会员手机 = t.会员手机 and 入会时间 > t.入会时间) order by t.会员手机
drop table tb
/*
会员卡号        入会时间                                                   会员手机                 
----------- ------------------------------------------------------ -------------------- 
6           2011-12-12 00:00:00.000                                13511111111
8           2012-01-07 00:00:00.000                                13777777777
9           2012-02-09 00:00:00.000                                13788888888
(所影响的行数为 3 行)
会员卡号        入会时间                                                   会员手机                 
----------- ------------------------------------------------------ -------------------- 
6           2011-12-12 00:00:00.000                                13511111111
8           2012-01-07 00:00:00.000                                13777777777
9           2012-02-09 00:00:00.000                                13788888888
(所影响的行数为 3 行)
*/