Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to fetch the total employee count beginning and end for the given period.

Example:
For Quarter 1,
Total emp count on 2020-04-01 and Total emp count on 2020-06-30.

I have the date of joining and date of leaving columns

What I have tried:

I achieve this by using below logic.

select count(*) from empmaster
where not in WHOEVER JOINED AFTER 2020-04-01 and WHOEVER LEFT AFTER 2020-04-01
Posted
Updated 22-Dec-20 4:43am
Comments
[no name] 21-Dec-20 15:45pm    
How about: where joinedCompany <= [date] and (leftCompany is null or >= [date]).
Virendra S from Bangalore, Karnataka 22-Dec-20 2:03am    
thanks @Gerry Schmitz , it's helped me.


There are a few things wrong / could be better with your solution.

1. Do not use convert! Your columns should be of type date or datetime and all operations on them should use dates not strings.

If you are concerned about the impact of time on these calculations either ensure that the time is stored as 00:00:00.0000 when entering data into the database or cater for the time in your query - i.e. use "start of day" or "end of day" as appropriate - see Lynn Pettis' examples[^]. Upgrading to SQL 2016 or later would be even better as the date column type was introduced

2. An alternative to using and/or with brackets (which often causes errors when the brackets get mismatched) you can using the ISNULL function [^] E.g.
SQL
select * from [dbo].[tblEmployeeMaster]
where doj <= '2020-04-01' and ISNULL(dol, '2020-04-01') >= '2020-04-01')
3. The logic for employees at the start of quarter shouldn't really include the first day of the quarter (it's that whole start of day / end of day thing again), so shouldn't that be
SQL
select * from [dbo].[tblEmployeeMaster]
where doj < '2020-04-01' and ISNULL(dol, '2020-04-01') >= '2020-04-01')
4. Instead of using hard-coded dates you probably want to introduce some parameters so you can vary the year or quarter (or any dates to define the period). For example
SQL
-- Date control variables / parameters
declare @startOfYear datetime = '2020-01-01'	-- The year we want
declare @QuarterRequired int = 1				-- e.g. 1st quarter of the year

-- don't actually need these, they just make the query a bit clearer
-- Start of the quarter @QuarterRequired
declare @startOfPeriod datetime = dateadd(qq, @QuarterRequired - 1, @startofYear)
-- End of the quarter @QuarterRequired
declare @endOfPeriod datetime = dateadd(d,-1,dateadd(q, 1, @startOfPeriod))
4. Finally, the original requirement was to have the counts of employees (although listing them as you have in your solution is a good way of checking the results before getting to the final query). You probably also want those figures returned in a single query so you are going to want to understand PIVOT - see this CP article Simple Way To Use Pivot In SQL Query[^]. Or for example
SQL
select * from
(
	select 'Beginning' as [when], count(*) as empcount from dbo.[tblEmployeeMaster]
	where doj < @startOfPeriod and (dol is null or dol > @startofPeriod)
	union
	select 'End', count(*) from dbo.[tblEmployeeMaster]
	where doj <= @endOfPeriod and isnull(dol, dateadd(d,1,@endOfPeriod)) > @endOfPeriod
) anchor
PIVOT
(
	max(empcount) for [when] in ([Beginning], [End])
) PVT
which (for my test data) gave
Beginning	End
3			4
 
Share this answer
 
for emp count at beginnning of the period--
select* from [dbo].[tblEmployeeMaster]
where convert(datetime,doj,103) <= '2020-04-01' and (convert(datetime,dol,103) is null or convert(datetime,dol,103) >= '2020-04-01')



for emp count at end of the period--
select* from [dbo].[tblEmployeeMasterLive]
where convert(datetime,doj,103) <= '2020-06-30' and (convert(datetime,dol,103) is null or convert(datetime,dol,103) >= '2020-06-30')
 
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