表名:test表数据如下:部门 姓名 入职时间
A jack 2010-1-29A K 2010-1-28B mike 2010-1-27B L 2010-1-26想取出每个部门入职时间最晚的那笔数据该如何取?结果如下:部门 姓名 入职时间
A jack 2010-1-29B mike 2010-1-27
A jack 2010-1-29A K 2010-1-28B mike 2010-1-27B L 2010-1-26想取出每个部门入职时间最晚的那笔数据该如何取?结果如下:部门 姓名 入职时间
A jack 2010-1-29B mike 2010-1-27
where not exists(
select 1 from test where 部门=t.部门 and 入职时间>t.入职时间)
select * from test where 入职时间 in
(
select max(入职时间) from test
)
SQL> with tab as
2 (
3 select 'A' 部门,
4 'jack' 姓名,
5 to_date('2010-1-29', 'yyyy-MM-dd') 入职时间
6 from dual
7 union all
8 select 'A', 'K', to_date('2010-1-28', 'yyyy-MM-dd')
9 from dual
10 union all
11 select 'B', 'mike', to_date('2010-1-27', 'yyyy-MM-dd')
12 from dual
13 union all
14 select 'B', 'L', to_date('2010-1-26', 'yyyy-MM-dd') from dual
15 )
16 select t1.* from tab t1,
17 (
18 select 部门,max(入职时间) 入职时间 from tab group by 部门
19 )
20 t2
21 where t1.部门=t2.部门 and t1.入职时间 = t2.入职时间
22 /部门 姓名 入职时间
---- ---- -----------
A jack 2010-1-29
B mike 2010-1-27SQL>