Skip to content
Advertisement

Building an Employee Date Matrix

I have a list of employees, along with list of cities they’ve worked in. I need to build a matrix (in SQL Server) of start/end dates by city to determine where they were at any given period in time.

The “end date” would be exactly the date before they appeared at a new location.

I’ve included an example of the source table, and what the output table should appear, along with the code that will build a temp table.

Any recommendations on how I can design this query, and what functions to use?

SOURCE:

Desired query output:

Advertisement

Answer

This is a classic Gaps-and-Islands.

Here we use a CROSS APPLY B to get the Date Range, and then CROSS APPLY C as an ad-hoc tally table

Example

Returns

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