实用但 好几个人没解决的SQL 查询 (小挑战一下),该怎么处理

实用但 好几个人没解决的SQL 查询 (小挑战一下)
select   min(enddate)   enddate   from   hm_em_contracts   C   where   C.em_id= '111222 '
group   by   C.em_id

这个查询没问题,但我还想多显示ID   这个字段,我写成这样
select   min(C.id))   ID   ,min(enddate)   enddate   from   hm_em_contracts   C   where   C.em_id= '111222 '
group   by   C.em_id

语句通过,但这个ID不是对应相关记录的值。   如下
        id         enddate
          5         2003-06-01
但应在数据库中的ID   是     7

怎   么解决?       谢谢!


------解决方案--------------------
搞得那么复杂干什么

简单点
select top 1 C.id ID,enddate from hm_em_contracts C where C.em_id= '111222 '
order by C.id
------解决方案--------------------
肯定不是你想要的了,min(C.id)) ID 是在结果中找最小的id,min(enddate) 是在所有结果中找最小的enddate,是打乱了找的,并不是一行记录,如果想找enddate最小的ID 的话 这样:

select distinct a.id,b.*
from hm_em_contracts a
inner join (
select C.em_id, min(enddate) enddate from hm_em_contracts C where C.em_id= '111222 '
group by C.em_id) b
on a.em_id=b.em_id and a.enddate=b.enddate
------解决方案--------------------
update hm_em_contracts set serialNO=C.code+ '01 '
from
(select min(enddate) enddate,em_id
from hm_em_contracts C group by em_id) T
inner join hm_employees C on C.em_id=T.em_id and c.enddate=t.enddate
where hm_em_contracts.id=C.id
------解决方案--------------------
d 前面 的括号里面没有 select 不是一个结果集,所以不能加别名
这样试试
update hm_em_contracts set serialNO= '01 '
from
(select C.em_id, min(enddate) enddate
from hm_em_contracts C group by C.em_id) b
inner join hm_em_contracts a
on a.em_id=b.em_id and a.enddate=b.enddate
where a.em_id is not null

不是要找 ID嘛? 最后一句也可以用 where a.id is not null