Daily snapshot table using cte loop

眉间皱痕 提交于 2019-12-11 17:24:32

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/53211232/daily-snapshot-table-using-cte-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!