需要只有一份工作的员工详细信息

时间:2015-10-28 12:08:17

标签: sql

需要只有一份工作的员工详细信息

例如这是表

ID    Name    StartDate    EndDate
1    Fischel    01-May-97    Jan-99
1    Fischel    08-May-92    02-Feb-99
1    Fischel    11-May-92    04-May-10
2    David    10-aug-1980    05-May-1981
3    John    12-sep-1988     06-June-2009
3    John    23-Aug-92    01-Nov-11

像这样输出

2    David    10-aug-1980    05-May-1981

2 个答案:

答案 0 :(得分:0)

    Select * from
    (select *,row_number() over (partition by id order by StartDate desc ) as rnm
    from your table
    )Derived_Table
    where Derived_Table.rnm=1;

OR

select * from table where id in (
select id
from table group by id having count(*) =1) z;

答案 1 :(得分:0)

如果相同的用户名没有任何其他日期,请使用NOT EXISTS返回用户。

select ID, Name, StartDate, EndDate
from tablename t1
where not exists (select 1 from tablename t2
                  where t2.id = t1.id
                    and (t2.StartDate <> t1.StartDate
                         or t2.EndDate <> t1.EndDate))

或者,有一个子查询返回所有只出现一次的id:

select ID, Name, StartDate, EndDate
from tablename t1
where id in (select id from tablename
             group by id
             having count(*) = 1)