Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please can any one help me
I have one table name as Empmaster in that table i have columns Emp_Joinindate ,Emp_leavingdate,Emp_Status
my question is
Suppose
the company hired 100 employee in the month of January 2012
and September 2012 50 employee left from the company
and October 2012 again company hired 25 new employees

here i am passing month and year soo i want show this data like how many employee presently working (Active Emp)and and how many Employee left the company on particular month of year


suppose if i pass month=jan and year =2012
PresentWorkingEmp=100 and leftEmp=0

suppose if pass month=sept and year =2012
PresentWorkingEmp=50 and leftEmp=50

suppose if pass month=October and year =2012
PresentWorkingEmp=75 and leftEmp=0 because (in month oct company hired 25 so 50+25=75 leftEmp=0)



please can any one help me
Posted
Updated 1-Apr-14 23:43pm
v3
Comments
Maciej Los 2-Apr-14 6:00am    
What have you tried till now? Where are you stuck?

Try this. If you have post the sample data.

SQL
declare @m varchar(2), @y varchar(4)
set @m = '08'
set @y=2012

DECLARE @date DATETIME
SELECT @date = convert(datetime, @m  + '-01' + '-' + @y)
--select @date
SELECT @date=DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @date) + 1, 0))

declare @Empmaster table (
Emp_Joinindate datetime,Emp_leavingdate datetime,Emp_Status varchar(2)
)

insert @Empmaster values ('04-01-2012','05-01-2012','1')
insert @Empmaster values ('04-01-2012',NULL,'1')
insert @Empmaster values ('05-01-2012',null,'1')
insert @Empmaster values ('05-01-2012','07-01-2012','1')
insert @Empmaster values ('06-01-2012',null,'1')
insert @Empmaster values ('07-01-2012',null,'1')

//Active emp
select * from @Empmaster
where Emp_Joinindate <= @date and (ISNULL(Emp_leavingdate,'12-31-9999')) >= @date

// Emp left
select * from @Empmaster
where Emp_Joinindate <= @date and (ISNULL(Emp_leavingdate,'12-31-9999')) <= @date
 
Share this answer
 
v2
Comments
Member 10562086 2-Apr-14 6:50am    
Sir Can u Explain
(ISNULL(Emp_leavingdate,'12-31-9999')) >= @date

And even i pass month and year separately,based on that month of year i want total no(Count of) of employee presently working and left Emp
ArunRajendra 2-Apr-14 6:56am    
1. Is null used to make the end date the into future. Else we need to write the condition as (Emp_leavingdate>= @date or Emp_leavingdate is null).
2. Yes you can pass Mont and year separately as I have used 2 variable one for month and one for year.
You can try this one too

SQL
SELECT SUM(CASE WHEN Emp_Status = 1 AND datepart(MONTH,[Emp_Joinindate]) = DATEPART(Month,GETDATE()) AND datepart(YEAR,[Emp_Joinindate]) = DATEPART(YEAR,GETDATE())
AND Emp_LeftDate IS NULL
THEN 1 ELSE 0 END) as WorkingEmp ,
SUM(CASE WHEN Emp_Status = 0 AND datepart(MONTH,[Emp_Joinindate]) = DATEPART(Month,GETDATE()) AND datepart(YEAR,[Emp_Joinindate]) = DATEPART(YEAR,GETDATE())
AND Emp_LeftDate IS NOT NULL
THEN 1 ELSE 0 END) as LeftEmployee ,
from EmployeeMaster

SQL

 
Share this answer
 
For Employees Who Are Working I am assuming That Their Leaving Date Is Null Obviously

Select * From Empmaster Where Emp_LeavindDate Is Null
And DatePart(mm,EmpJoinInDate) = 'MONTH PARAMETER HERE In Numeric'
And DatePart(yy,EmpJoinInDate) = 'YEAR MONTH PARAMETER HERE In Numeric'


For Employees Who Have Left I am assuming That Their Leaving Date Is Not Null Obviously

Select * From Empmaster Where Emp_LeavindDate Is Not Null
And DatePart(mm,EmpLeavingDate) = 'MONTH PARAMETER HERE In Numeric'
And DatePart(yy,EmpLeavingDate) = 'YEAR MONTH PARAMETER HERE In Numeric'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900