Solved

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

Posted on 2014-11-17
6
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 66

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 48

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 66

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 66

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

724 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