EMPTable
name age gender createdate
张三 15 男 2011-12-5
张三 16 男 2011-12-6
张三 15 男 2011-12-9
李四 14 女 2011-12-3
李四 14 女 2011-12-15
查询 某人 最后一次创建的时间.(条件是姓名和年龄)正确显示:
name age gender createdate
张三 15 男 2011-12-9
张三 16 男 2011-12-6
李四 14 女 2011-12-5
name age gender createdate
张三 15 男 2011-12-5
张三 16 男 2011-12-6
张三 15 男 2011-12-9
李四 14 女 2011-12-3
李四 14 女 2011-12-15
查询 某人 最后一次创建的时间.(条件是姓名和年龄)正确显示:
name age gender createdate
张三 15 男 2011-12-9
张三 16 男 2011-12-6
李四 14 女 2011-12-5
(
select max(createdate) from EMPTable
where name =a.name and age=a.age
)
create table EMPTable
(name char(6), age int, gender char(4), createdate date)insert into EMPTable
select '张三', 15, '男', '2011-12-5' union all
select '张三', 16, '男', '2011-12-6' union all
select '张三', 15, '男', '2011-12-9' union all
select '李四', 14, '女', '2011-12-3' union all
select '李四', 14, '女', '2011-12-15'select a.*
from EMPTable a
inner join
(select name,age,max(createdate) mcreatedate
from EMPTable
group by name,age) b
on a.name=b.name and a.age=b.age and a.createdate=b.mcreatedate
order by a.name desc,a.agename age gender createdate
------ ----------- ------ ----------
张三 15 男 2011-12-09
张三 16 男 2011-12-06
李四 14 女 2011-12-15(3 row(s) affected)
select max(createdate) from EMPTable group by age
)--张三 16 男 2011-12-06 00:00:00.000
--张三 15 男 2011-12-09 00:00:00.000
--李四 14 女 2011-12-15 00:00:00.000
(name char(6), age int, gender char(4), createdate datetime)--查询 某人 最后一次创建的时间.(条件是姓名和年龄)insert into EMPTable
select '张三', 15, '男', '2011-12-5' union all
select '张三', 16, '男', '2011-12-6' union all
select '张三', 15, '男', '2011-12-9' union all
select '李四', 14, '女', '2011-12-3' union all
select '李四', 14, '女', '2011-12-15'insert into EMPTable
select '张三',15,'女','2011-12-2'select * from EMPTable a where createdate=
(
select max(createdate) from EMPTable
where name =a.name and age=a.age and gender = a.gender
)orselect * from EMPTable a where createdate in(
select max(createdate) from EMPTable group by age
)