Solved

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

Posted on 2014-11-17
6
64 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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

10 Experts available now in Live!

Get 1:1 Help Now