I have a table, employee history with the following
ID | Current_Dept | Dept_Start_Date | Name
1 | Sales | 2012-01-01 | John Smith
1 | Marketing | 2010-01-01 | John Smith
1 | Intern | 2008-01-01 | John Smith
2 | IT | 2012-01-01 | Sue Jones
2 | Sales | 2011-01-01 | Sue Jones
2 | eBusiness | 2010-10-01 | Sue Jones
3 | Warehouse | 2012-01-01 | Bobby Ray
3 | Sales | 2009-01-01 | Bobby Ray
What I would like is a query giving everyone who ever worked in Sales:
ID | DeptBefore | DeptAfter | CurrentDept
1 | Marketing | | Sales
2 | ebusiness | IT | IT
3 | | Warehouse | Warehouse
At the moment I see no simpler way of doing this other then recursively querying the table for each row.
I’m tempted to create a ‘temp’ table that’s populated once a day and run the query off of that.
I’m using SQL Server and C# (ASP.NET) incase these offer other simpler solutions.
I hope that all makes sense.
Thanks,
From your data it appears that you want the departments
immediatelybefore and after the sales department. For example; you don’t show that John Smith started as an intern.NOTE: You may want to consider the results you want if someone has worked for sales on two different occasions.
By ordering the items in Descending order, the current record is always
sequence_id = 1.