select
a.deptname,b.*
from
部门表 a,
员工表 b
where
a.deptid=b.deptid
and
not exists(select 1 from 员工表 where deptid=a.deptid and age>a.age)
a.deptname,b.*
from
部门表 a,
员工表 b
where
a.deptid=b.deptid
and
not exists(select 1 from 员工表 where deptid=a.deptid and age>a.age)
a.deptname,b.*
from
部门表 a,
员工表 b
where
a.deptid=b.deptid
and
b.empid=(select top 1 WiTh Ties empid from 员工表 where deptid=a.deptid order by age DESC)
-------------------------------------------------------------------------------------------------------------------------
select
a.deptname,b.*
from
部门表 a,
员工表 b
where
a.deptid=b.deptid
and
not exists(select 1 from 员工表 where deptid=b.deptid and age>b.age)
select a.* from 表 a where not exists (select * from 表 where 最大判断的条件>a.最大判断的条件 and a.分组条件1=分组条件1 and a.分组条件2=分组条件2 and .....)
最小的
select a.* from 表 a where not exists (select * from 表 where 最大判断的条件<a.最大判断的条件 and a.分组条件1=分组条件1 and a.分组条件2=分组条件2 and .....)
a.deptname,b.*
from
部门表 a,
员工表 b
where
a.deptid=b.deptid
group by a.deptname
having max(age)
我也是做人事系统的,经常碰到类似的情况,我一般的处理方法是这样的:
-----------------------------------
1、先把部门表和员工表做左外连接,得出每个部门的员工的详细信息后,放到一张临时表里
2、对临时表按DepID分组,取出DepID以及对应的Max(age)
3、用上面查询的结果再与临时表做连接,找出DepID相等,且年龄=Max(age)的记录即可。Select *
Into #Temp
From 部门表 d Left Join 员工表 e
On d.DepID = e.DepIDSelect t.* --这里可以根据需要,取得相应的字段
From #Temp t,
(
Select DepID,Max(age) maxage
From #Temp t1
Group By DepID
) t2
Where t.DepID = t2.DepID And t.adge = t2.maxage