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-06-11 : 04:43:11
|
Dear All , I am looking for a query where i can get month wise total number of employeesuppose i have 100 employee in mar-12and 15 new joiend in same month so in april i should have 115 employeemy query isselect SUM(a.total) emp from (select count(emp_staffid) total,left(DATEname(mm,EMP_DATEOFJOINING),3)+'-'+right(convert(varchar,DATEPART(yy,EMP_DATEOFJOINING)),2) "Month-Year" from ERM_EMPLOYEE_MASTERwhere EMP_ISACTIVE like '%1%'group by left(DATEname(mm,EMP_DATEOFJOINING),3)+'-'+right(convert(varchar,DATEPART(yy,EMP_DATEOFJOINING)),2))awhere a.[Month-Year]='apr-12' which is not giving me exact outputregards, Vipin jha |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-06-11 : 05:11:43
|
[code]-- Prepare staging tableCREATE TABLE #Stage ( MonthNumber INT, Joined INT )-- Populate staging tableINSERT #Stage ( MonthNumber, Joined )SELECT DATEDIFF(MONTH, '19000101', Emp_DateOfJoining) AS MonthNumber, COUNT(*) AS JoinedFROM dbo.Erm_Employee_MasterWHERE Emp_IsActive = 1GROUP BY DATEDIFF(MONTH, '19000101', Emp_DateOfJoining)-- Display the final resultSELECT SUBSTRING(CONVERT(VARCHAR(9), DATEADD(MONTH, s.MonthNumber, '19000101'), 6), 4, 6) AS MonthName, f.JoinedFROM #Stage AS sCROSS APPLY ( SELECT SUM(x.Joined) FROM #Stage AS x WHERE x.MonthNumber <= s.MonthNumber ) AS f(Joined)ORDER BY s.MonthNumber-- Clean upDROP TABLE #Stage[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
vipinjha123
Starting Member
45 Posts |
Posted - 2012-06-11 : 07:22:56
|
Dear friend thankx for your replymy query is SELECT TABA.* FROM (select a.[Month-Year] "Year-Month of joinee/Exit",SUM(a.[Number of joined Empl]) "Count of joined Emp" ,sum(a.[left emp]) as "Count of Exit emp" from (selectleft(DATEname(mm,EMP_DATEOFJOINING),3)+'-'+right(convert(varchar,DATEPART(yy,EMP_DATEOFJOINING)),2) "Month-Year", count(EMP_DATEOFJOINING) as "Number of joined Empl",0 as "left emp"from erm_employee_master left join SEP_EMPLOYEE_SEPARATION on SEP_EMPLOYEE_SEPARATION.ES_EMP_STAFFID=erm_employee_master.EMP_STAFFIDWHERE EMP_ISACTIVE='1'group by left(DATEname(mm,EMP_DATEOFJOINING),3)+'-'+right(convert(varchar,DATEPART(yy,EMP_DATEOFJOINING)),2)UNION ALLselectleft(DATEname(mm,ES_LAST_WORKING_DATE),3)+'-'+right(convert(varchar,DATEPART(yy,ES_LAST_WORKING_DATE)),2) "Month-Year",0,count(*) as "Number of exit Empl"from SEP_EMPLOYEE_SEPARATION inner join ERM_EMPLOYEE_MASTER on ERM_EMPLOYEE_MASTER.EMP_STAFFID=SEP_EMPLOYEE_SEPARATION.ES_EMP_STAFFIDwhere ERM_EMPLOYEE_MASTER.EMP_ISACTIVE='0'group by dateadd(month, datediff(month, 0, ES_LAST_WORKING_DATE), 0),left(DATEname(mm,ES_LAST_WORKING_DATE),3)+'-'+right(convert(varchar,DATEPART(yy,ES_LAST_WORKING_DATE)),2))awhere a.[Month-Year] IN ('JAN-12','FEB-12','MAR-12','APR-12','MAY-12')group by a.[Month-Year])TABAorder by TABA.[Year-Month of joinee/Exit] desci want to see data on ascenbding order(month-year) wiselike jun-12may-12apr-12mar-12i am unable to get itplease suggest regards,vipin jha |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-11 : 15:42:48
|
for getting data in order you want add ORDER BY likeORDER BY DATEPART(yy,datefield) DESC,DATEPART(mm,datefield) DESC------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|