Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mvijayrkumar
Starting Member
6 Posts |
Posted - 2008-12-20 : 07:13:35
|
| i Have a table named "Demographics" which contain the Employee Details such as Employee Code ,HireDate of which "HireDate" is Employee Hire Date in (mm/dd/yyyy,12:00:00 AM format).The table also contained the resigned Employee Details. Demographics TableEmpCode HireDate-------- ---------ATA01 12/03/2006ATA02 12/03/2006ATA04 12/03/2006 ATA03 12/03/2006I have another table named "Employee End Of Service" which has the fields Employee Code,(EOSDate)End Of Service Date.Employee End Of Service TableEmpCode HireDate-------- ---------ATA01 12/10/2007I want to find the Total Number of Employees having Working Span of >10yrs7-10yrs,4-6yrs,1-3yrs,<1yrFirst we have to check whether any data exists in Employee End Of Service" Table,if yes means we can calculate ((EOSDate from "Employee End Of Service") -(HireDate from Demographics))if "No" means (CurrentDate-HireDate)..SQL Query Results Should beRange Total Number of Employees----- -------------------->10 2 7-10 44-6 61-3 9< 1 12Guys please help me how to do this in SQL Server 2005..... |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-20 : 08:58:00
|
[code]create table #Demographics(EmpCode char(5),HireDate datetime)create table #Employee_End_Of_Service(EmpCode char(5),EOSDate datetime)insert #Demographicsselect 'ATA01', '12/03/2006' union allselect 'ATA02', '12/03/2006' union allselect 'ATA04', '12/03/1999' union allselect 'ATA03', '12/03/2006' union allselect 'ATA05', '12/03/1980' union allselect 'ATA06', '12/03/2004' union allselect 'ATA07', '12/03/1995'insert #Employee_End_Of_Serviceselect 'ATA01', '12/10/2007' union allselect 'ATA02', '12/10/2006' union allselect 'ATA07', '12/10/1999'select * from #Demographicsselect * from #Employee_End_Of_Service-- here begins the solutionselect'> 10' as Range,count(*) as [Total Number of Employees]from#Demographics dleft join #Employee_End_Of_Service eon (d.EmpCode = e.EmpCode)where datediff(year, d.HireDate, isnull(e.EOSDate,dateadd(d,datediff(d,0,getdate()),0))) > 10union allselect'7 - 10' as Range,count(*) as [Total Number of Employees]from#Demographics dleft join #Employee_End_Of_Service eon (d.EmpCode = e.EmpCode)where datediff(year, d.HireDate, isnull(e.EOSDate,dateadd(d,datediff(d,0,getdate()),0))) between 7 and 10union allselect'4 - 6' as Range,count(*) as [Total Number of Employees]from#Demographics dleft join #Employee_End_Of_Service eon (d.EmpCode = e.EmpCode)where datediff(year, d.HireDate, isnull(e.EOSDate,dateadd(d,datediff(d,0,getdate()),0))) between 4 and 6union allselect'1 - 3' as Range,count(*) as [Total Number of Employees]from#Demographics dleft join #Employee_End_Of_Service eon (d.EmpCode = e.EmpCode)where datediff(year, d.HireDate, isnull(e.EOSDate,dateadd(d,datediff(d,0,getdate()),0))) between 1 and 3union allselect'< 1' as Range,count(*) as [Total Number of Employees]from#Demographics dleft join #Employee_End_Of_Service eon (d.EmpCode = e.EmpCode)where datediff(year, d.HireDate, isnull(e.EOSDate,dateadd(d,datediff(d,0,getdate()),0))) < 1-- here ends the solutiondrop table #Demographicsdrop table #Employee_End_Of_Service[/code]Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
mvijayrkumar
Starting Member
6 Posts |
Posted - 2008-12-21 : 08:15:31
|
HI webfred...i really thank and appreciate u for the effort u had taken to solve the issue..The Query worked am really happy and greateful to u....im a new member to sqlteam..hope will meet u soon.. Thanks once again.. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-21 : 10:32:21
|
you are welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|