Skip to content
Advertisement

Daily snapshot table using cte loop

I need daily snapshot of how many employees are employed on any given day (defined as @date between Start_Date & End_Date), with one line per date.

At one point, the below script would return all daily snapshots, but they were each returned within a separate results window. I couldn’t get the query to combine all results to insert each new result into #PLEASEWORK.

I need to get the below to work for two consecutive days. It uses int dates. I have inherited this problem and am building a date table to work around that problem.

CREATE TABLE #PLEASEWORK
(
    [Date] INT
    ,Division_Description VARCHAR (100)
    ,[Count] INT
)

DECLARE @cnt AS INT
DECLARE @DATE AS INT

SET @CNT = '20181001'

WHILE @cnt >= @DATE

BEGIN

SELECT @DATE = '20181031' - 1


TRUNCATE TABLE #PLEASEWORK
INSERT INTO #PLEASEWORK

SELECT
    @DATE [date]
    ,Division_Description
    ,COUNT(*) [Count]
--INTO #PLEASEWORK
FROM EmpMaster

WHERE
    ([Start_Date] <= @DATE
    AND Leaving_Date = 0)
    OR ([Start_Date] <= @DATE
    AND Leaving_Date > @DATE)

GROUP BY
    Division_Description


SET @cnt = @cnt + 1

CONTINUE


SELECT *

FROM #PLEASEWORK

END

Advertisement

Answer

You can do all this in one query. Here is one method (which is not the most efficient), but should be fine for one month.

declare @DATESTART date = '20181001';
declare @DATEEND date = '20181031';

with dates as (
      select @DATESTART as dte
      union all
      select dateadd(day, 1, dte)
      from dates
      where dte < @DATEEND
     )
select d.dte, dd.Division_Description,
       (select count(*)
        from EmpMaster e
        where e.Division_Description = dd.Division_Description and
              e.start_date <= d.dte and
              (e.leaving_date >= d.dte or e.leaving_date is null)
       ) as num_employees
from dates d cross join
     (select distinct Division_Description from EmpMaster) dd
order by d.dte, dd.Division_Description;

With an index on EmpMaster(Division_Description, start_date, leaving_date), this should be much better than a cursor-based approach.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement