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)

解决方案 »

  1.   

    select
        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)
      

  2.   

    在前边写错别名了:
    -------------------------------------------------------------------------------------------------------------------------
    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)
      

  3.   

    按条件分组找出每组最大的数据:
    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 .....) 
      

  4.   

    select
        a.deptname,b.*
    from
        部门表 a,
        员工表 b
    where
        a.deptid=b.deptid
    group by a.deptname 
    having max(age)
      

  5.   

    libin_ftsafe(子陌红尘)的方法是可行的.但是如果员工表的数据量比较大的话,速度会比较慢.
    我也是做人事系统的,经常碰到类似的情况,我一般的处理方法是这样的:
    -----------------------------------
    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