Solved

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

Posted on 2014-11-17
6
67 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

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