You need to normalize your tables.-- prepare test datadeclare @test table (Employee_ID int, Transaction_Date datetime, Job_Code tinyint, Job_Title varchar(8))insert @testselect 10070, '11/5/2004', 104, 'Staff' union allselect 10070, '5/6/2005', 104, 'Staff' union allselect 10070, '5/20/2005', 108, 'Manager' union allselect 10070, '7/29/2005', 108, 'Manager' union allselect 10070, '9/9/2005', 108, 'Manager' union allselect 10070, '1/1/2006', 121, 'Director' union allselect 10070, '3/1/2006', 121, 'Director'-- show test dataselect * from @test-- Do the work!select t0.*, ( select case when min(transaction_date) is null then dateadd(day, datediff(day, 0, getdate()), 0) else min(transaction_date) - 1 end from @test t1 where t1.employee_id = t0.employee_id and t1.transaction_date > t0.begindate and t1.job_code <> t0.job_code and t1.job_title <> t0.job_title ) enddatefrom ( select employee_id, min(transaction_date) begindate, job_code, job_title from @test group by employee_id, job_code, job_title ) t0order by t0.employee_id, t0.begindate
Peter LarssonHelsingborg, Sweden