日期:2014-05-16 浏览次数:20916 次
--> 测试数据: [ta]
if object_id('[ta]') is not null drop table [ta]
go
create table [ta] ([employee_number] int,[last_name] varchar(4),[organization_id] varchar(12),[position_name] varchar(12),[effective_start_date] datetime,[effective_end_date] datetime)
insert into [ta]
select 8610112,'张三','办公室秘书科','文秘综合管理','2003-7-1','2007-10-31' union all
select 8610112,'张三','办公室秘书科','文秘综合管理','2007-11-1','2008-2-29' union all
select 8610112,'张三','办公室秘书科','文秘综合管理','2008-3-1','2008-12-31' union all
select 8610112,'张三','办公室秘书科','文秘综合管理','2009-1-1','2009-2-28' union all
select 8610112,'张三','办公室秘书科','文秘综合管理','2009-3-1','2009-3-30' union all
select 8610112,'张三','办公室秘书科','文秘综合管理','2009-3-31','2009-7-12' union all
select 8610112,'张三','计划推进科','投资项目管理','2009-7-13','2010-3-30' union all
select 8610112,'张三','计划推进科','投资项目管理','2010-3-31','2010-6-9' union all
select 8610112,'张三','经营管理科','统计','2010-6-10','2010-10-30' union all
select 8610112,'张三','经营管理科','统计','2010-10-31','2011-3-31' union all
select 8610112,'张三','经营管理科','副科长','2011-4-1','2011-8-31' union all
select 8610112,'张三','经营管理科','副科长','2011-9-1','2012-9-29' union all
select 8610112,'张三','经营管理科','副科长','2012-9-30','2013-11-30' union all
select 8610112,'张三','经营管理科','副科长','2013-12-1','2013-12-1' union all
select 8610112,'张三','经营管理科','副科长','2013-12-2','4712-12-31'
;with
wang1 as(
select * from [ta] s
where not exists(select 1 from ta where employee_number=s.employee_number and position_name=s.position_name and effective_start_date<s.effective_start_date)
),
wang2 as(
select * from [ta] s
where not exists(select 1 from t