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.

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.

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