SQL中的最新日期

时间:2017-02-14 07:37:23

标签: sql oracle11g

表员工

ID   Name  Update_date Active Phone_no
1    Dave  02-07-14     Y      99999945
1    Dave  19-12-16     Y      88888888
2    Mike  12-11-17     Y      234234567
2    Mike  12-11-14     Y      345435343

我需要的是:从员工中选择*,其中id为(1,2); 上面的查询会给我上面的表格。 但我需要的是

ID   Name  Update_date Active Phone_no
1    Dave  19-12-16     Y      88888888
2    Mike  12-11-17     Y      234234567

对于搜索的每个ID,它应该采用最新日期。

2 个答案:

答案 0 :(得分:1)

你可以使用元组和subselect with in子句和group by

select * from my_table 
where ( id, Update_date) in (  select  id, max(update_date)
                               from my_table
                               group by id)

答案 1 :(得分:0)

Chedk this。

 select * from 

 (    Select  ID,Name,row_number() over (partition by ID order by Update_date desc ) as row ,Active ,Phone_no ,
        Update_date 

        from employee  
)  a where id in (1,2) and row='1'