I have two tables that allocate employees to cost centers. The first table (CCmain
) shows the primary cost center. If an employee is assigned to more than one cost center, the second table (CCfraction
) shows what fraction of the employee is assigned to which cost center.
For a BI evaluation, I need the splitting in witch timespan an employee is in which cost center.
What query will give this result?
This illustrates one situation:
Time
CCmain: <---><-------------><----------> <--->
CCfraction: <---> <----><--------------->
Result: <---><---><---><---><----><----> <--->
Sample Data
CREATE TABLE CCmain(
ValidFrom date NOT NULL,
ValidTo date NOT NULL,
Costcenter int NOT NULL,
PN int NULL
);
CREATE TABLE CCfraction(
PN int NOT NULL,
ValidFrom date NOT NULL,
ValidTo date NOT NULL,
Costcenter int NOT NULL,
Fraction int NOT NULL
);
GO
INSERT INTO CCmain (ValidFrom,ValidTo,Costcenter,PN) VALUES
('2020-10-01','2020-12-31',100,1),
('2021-01-01','2021-12-31',200,1),
('2022-01-01','2022-09-30',100,1),
('2023-01-01','2023-03-31',300,1);
INSERT INTO CCfraction (PN,ValidFrom,ValidTo,Costcenter,Fraction) VALUES
(1,'2021-05-01','2021-07-31',200,60),
(1,'2021-05-01','2021-07-31',300,40),
(1,'2022-01-01','2022-06-30',100,70),
(1,'2022-01-01','2022-06-30',300,30),
(1,'2022-07-01','2023-12-31',100,55),
(1,'2022-07-01','2023-12-31',200,45);
GO
Desired output
PN Costcenter Fraction ValidFrom ValidTo
-- ---------- -------- ---------- ----------
1 100 100 2020-10-01 2020-12-31 CC 100 no split
1 200 100 2021-01-01 2021-04-30 CC 200 no split
1 300 40 2021-05-01 2021-07-31 CC 300 40% and
1 200 60 2021-05-01 2021-07-31 60% CC 200
1 200 100 2021-08-01 2021-12-31 CC 200 no split
1 100 70 2022-01-01 2022-06-30 CC 100 70% and
1 300 30 2022-01-01 2022-06-30 30% CC 300
1 100 55 2022-07-01 2022-09-30 CC 100 55% and
1 200 45 2022-07-01 2022-09-30 45% CC 200
1 100 55 2023-01-01 2023-03-31 CC 100 55% and
1 200 45 2023-01-01 2023-03-31 45% CC 200
From 2022-10-01 until 2022-12-31 is no CC, although there is a fraction.
The case is similar to this one, but it goes a little further when a cost center allocation is within a longer period of time.
Technical background
As requested, a bit more background.
To explain the background in more detail. The tables come from a personnel management software. There are actually even more tables involved in this problem, but I wanted to reduce the example to a minimum.
Each employee is assigned to at least one cost center. This should be represented with the CCmain table. There are employees who are assigned to several cost centers. The allocation of the employee is then entered in the CCfraction table. There are then at least two entries in the CCfraction table. One for the main cost center and one for the other cost center to which the employee is allocated. It is also possible for an employee to be allocated to three or more cost centers. In this case, there would be more entries.
What I have removed in the reduction to this example is that there is another table between the CCmain and CCfraction tables. This table allows several employees to use the same cost center splitting. As in this example, it is possible that an employee does not have a master cost center, but the allocation continues to run because it can be valid for other employees. This is the technical background. Sorry that I have reduced the example to a minimum.