?
Solved

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

Posted on 2014-11-17
6
Medium Priority
?
72 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
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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

770 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