Solved

SQL calculate years from view

Posted on 2011-03-24
2
265 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

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

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

828 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