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 |
vipinjha123
Starting Member
45 Posts |
Posted - 2012-05-30 : 04:02:12
|
Dear All,I am looking for a query which will give me number of employee join in month nad number of employee exit in same monthtable trucure is:-if employee join then u will get rfrom EMPLOYEE TABLEEMPLOYEE(EMPID,DTEOFJoINING,DEPT,LOCATION)If employee exit you will get details from SEPERATION(EMPID,lastdayofworking)i am looking a report output likemonth-year numofjoineee numofexit location deptthankx regrds,Vipin jha |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-05-30 : 07:53:28
|
;with cte as(select yearmth = convert(datetime,convert(varchar(6),getdate(),112) + '01')union allselect yearmth = dateadd(mm,-1,yearmth) where yearmth > '20100101') ,emp as (select dte = convert(datetime,convert(varchar(6),DTEOFJoINING,112) + '01') from EMPLOYEE) ,sep = (select dte = convert(datetime,convert(varchar(6),lastdayofworking,112) + '01') from SEPERATION)select cte.yearmth, numjoin = d1.num, numexit = d2.numfrom cte cleft join (select dte, num = count(*) from emp group by dte) d1on cte.yearmth = d1.dteleft join (select dte, num = count(*) from sep group by dte) d2on cte.yearmth = d2.dteThen you just need to add the location - will need to join to employee in the separation cte to get that value.Do you want zeros in the result for locations? In that case create a locations cte and cross join to it in the result.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|