从同一表中获取不同值的SQL查询



假设我有这样一个表:employeetable:

tbody> <<tr>2124
empid joindate location HRupdatedate Changes
12002-01-05德里2021-03-01
2009-09-09孟买2021-03-05
2010-06-27海德拉巴2021-03-03转移
2015-11-02德里2021-03-06转移
32020-01-01孟买2021-03-06
2007-07-30德里2021-03-04

您可以通过使用WHERE子句轻松实现这一点。

select * from  
employeetable et1
where 
joindate = (
select max(joindate)
from employeetable et2 
where et1.empid = et2.empid
) 
and 
empid in (1,2, 4);

或者得到更精确的结果

select * from  
employeetable et1
where 
joindate = (
select max(joindate)
from employeetable et2 
where et1.empid = et2.empid
) 
and 
HRupdatedate = (
select max(HRupdatedate)
from employeetable et2 where et1.empid = et2.empid
)
and 
empid in (1,2, 4);

您可以使用top with ties

select top(1) with ties *
from employeetable
where empid in (1,2,4)
order by row_number() over(partition by empid order by joindate desc)

相关内容

  • 没有找到相关文章

最新更新