写个SQL
表AA
结构如下:
name dt age
A 2001/01/02 65
A 2002/01/02 45
A 2003/01/02 44
B 2001/01/02 2
B 2002/01/02 5
要求找出
A 2003/01/02 44
B 2002/01/02 5
(日期最大的所有记录)
表AA
结构如下:
name dt age
A 2001/01/02 65
A 2002/01/02 45
A 2003/01/02 44
B 2001/01/02 2
B 2002/01/02 5
要求找出
A 2003/01/02 44
B 2002/01/02 5
(日期最大的所有记录)
insert into t_user (a,b,c)
select 'A','2001-01-02','65'
union
select 'A','2002-01-02','45'
union
select 'A','2003-01-02','44'
union
select 'B','2001-01-02','2'
union
select 'B','2002-01-02','5'select t.* from t_user t
where 2>(select count(*) from t_user where a=t.a and b>=t.b)
drop table t_user
where not exists (
select 1 from aa
where name=a.name
and dt>a.dt
)
create table t_user(a varchar(10) , b varchar(10) , c varchar(10))
insert into t_user (a,b,c)
select 'A','2001-01-02','65'
union
select 'A','2002-01-02','45'
union
select 'A','2003-01-02','44'
union
select 'B','2001-01-02','2'
union
select 'B','2002-01-02','5'select t.* from t_user t
where t.b=(select max(b) from t_user where a=t.a)