Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I may be looking at this too simplistically given all the talk about slowly changing dimensions, etc, but what's wrong with a plain old bit of SQL? I'm going to assume you have an Employee table with Manager, ContractedSite, StartDate and EndDate fields.</p> <p>You want the following:</p> <ul> <li>Current staff. Is this current at the start of the period or at the end of the period or active at some time during the period? </li> <li>Starters. People who have a StartDate greater than or equal to the start of the period but less than or equal to the end of the period.</li> <li>Leavers. People who have an EndDate greater than or equal to the start of the period but less than or equal to the end of the period.</li> <li>Turnover. Which brings us back to what you mean by "Current staff". Turnover is essentially the difference between the current staff at the start of the period and the current staff at the end of the period, expressed either as a raw number or a percentage. So current staff at the start of the period are people with a StartDate less than the period start and either no EndDate or an EndDate greater than or equal to the start of the period. Similarly with the current staff at the end of the period. </li> </ul> <p>So you have some specific cases to extract from your data:</p> <pre><code>SELECT Manager, ContractedSite, SUM(CASE WHEN StartDate &lt; @PeriodStart AND ((EndDate IS NULL) OR (EndDate &gt;= @PeriodStart)) THEN 1 END) AS OpeningStaff, SUM(CASE WHEN StartDate &gt;= @PeriodStart AND StartDate &lt;= @PeriodEnd THEN 1 END) AS Starters, SUM(CASE WHEN EndDate &gt;= @PeriodStart AND EndDate &lt;= @PeriodEnd) THEN 1 END) AS Leavers, SUM(CASE WHEN StartDate &lt;= @PeriodEnd AND ((EndDate IS NULL) OR (EndDate &gt; @PeriodEnd)) THEN 1 END) AS ClosingStaff FROM Employee WHERE (StartDate &lt;= @PeriodEnd) AND ((EndDate IS NULL) OR (EndDate &gt;= @PeriodStart)) GROUP BY Manager, ContractedSite ORDER BY Manager, ContractedSite </code></pre> <p>Now you just need to calculate the difference between OpeningStaff and ClosingStaff in your report to get the turnover and you're done. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload