Solved

How to find first startdate and last enddate for multiple-position employees for all departments

Posted on 2014-11-17
6
66 Views
Last Modified: 2014-11-17
We have several employees with multiple positions; some subsequent in same department, some in different departments (or a mix of both).

I need to find for each employee the first startdate and last enddate for their positions in each department (ie group by employee and dep)

Simplified tables:
Emp: empID, name
Dep: depID, name
Positions: empID, DepID, startdate, enddate
0
Comment
Question by:fmsol
  • 3
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40447735
Not entirely sure I'm understanding all of your requirements, but for starters give this a whirl
SELECT e.name as EmployeeName, d.name as DepartmentName, Min(startdate), max(enddate) 
FROM Positions p
   JOIN Emp e ON p.empID = e.EmpID
   JOIN Dep d ON p.depID = d.depID
GROUP BY e.name, d.name

Open in new window

If you'd like some more reading on GROUP BY I have an image and code-heavy tutorial out there called SQL Server GROUP BY Solutions
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40447745
Are you interested in knowing whether there were any employment gaps?

I assume that your Positions table will not allow anyone (EmpID) to be assigned to more than one department (DepID) at any time.  How are you dealing with situations where they start in one department, move to another, then move back to the original?
0
 

Author Comment

by:fmsol
ID: 40447763
Thank you Jim; of course, so simple.
But to make it more complicated: yes Dale, I need to know if there are employment gaps
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:fmsol
ID: 40447772
... and our Positions table will allow more than one department at the same time (this is hospital employees)
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40447779
Got an article for that one too (insert snarky remark here about no social life):  T-SQL: Identify bad dates in a time series.  Copy-paste the code from the article into your SSMS, execute it to see that it detects overlapping rows, then substitute your code above.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40447876
Thanks for the grade.  Good luck finding the overlaps.  -Jim
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now