Solved

SQL calculate years from view

Posted on 2011-03-24
2
255 Views
Last Modified: 2012-05-11
I have a view that has a
EmployeeNumber, companyname, startdate, and enddate

I need to find a way to calculate years of experience for each employeenumber when they worked at the company MYCOMPANY, and also a total of all years worked at another company.

I don't care it this ends up being two rows in the same view for each employeenumber (only one if they don't have other experience), or two different views.



SELECT     EmpNo, CompanyName, Location, Title, StartDate, EndDate
FROM         dbo.EmpWorkHistory AS e
GROUP BY EmpNo, StartDate, EndDate, CompanyName, Location, Title
ORDER BY EmpNo, StartDate

so
1, Google, 1990-1-1, 1991-1-1
1, IBM, 1991-1-1, 1992-1-1
1, XXX, 1992-1-1, 1993-1-1
1, MyCompany, 1993-1-1, 1994-1-1

would give me
1, MyCompany, 1 Year
1, AllOtherCompanies, 3 Years

Thanks!
0
Comment
Question by:jackjohnson44
  • 2
2 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 35211038
Is this what you want?

SELECT CASE Company WHEN 'MyCompany' THEN 'MyCompany' ELSE 'AllOtherCompanies' END as Company,
       SUM(DATEDIFF(yy, StartDate, EndDate)) as YearsExperience
  FROM dbo.EmpWorkHistory AS e
GROUP BY CASE Company WHEN 'MyCompany' THEN 'MyCompany' ELSE 'AllOtherCompanies' END

Open in new window

0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 500 total points
ID: 35211069
Wow - I don't know what I was thinking - that's not at all what you want. Here's what you're looking for, and can do it in a single row per employee:

SELECT EmpNo, SUM(CASE Company WHEN 'MyCompany' THEN DATEDIFF(yy, StartDate, EndDate) ELSE 0 END) as YearsWithUs,
       SUM(CASE Company WHEN 'MyCompany' THEN 0 ELSE DATEDIFF(yy, StartDate, EndDate) END) as YearsSomewhereElse
  FROM dbo.EmpWorkHistory AS e
GROUP BY EmpNo

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

759 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

17 Experts available now in Live!

Get 1:1 Help Now