In my table I have data from employee contracts like this:
And I want to get job position in time periods, something like this
login | ValidFrom | ValidTo | JobPosition |
---|---|---|---|
bcde | 2019-07-01 | 2019-09-30 | Project Manager |
bcde | 2020-01-09 | 2020-06-16 | Head of Center of Excellence |
bcde | 2020-06-17 | 2021-07-31 | Team Leader |
bcde | 2021-08-01 | 2099-12-31 | Head of Center of Excellence |
So I write query:
select DimEmployeeId, JobPosition, login, min(ValidFrom), max(ValidTo)
from employeeContracts
group by DimEmployeeId, JobPosition, login
But It doesn't work in this case (if someone has the same JobPosition), so I decided to use dense_rank, like this:
select login, ValidFrom, ValidTo, JobPosition
,dense_rank() OVER (Partition BY JobPosition ORDER BY login, ValidFrom, ValidTo, JobPosition) as no
from employeeContracts
and after that
select DimEmployeeId, JobPosition, login, min(ValidFrom), max(ValidTo)
from employeeContracts
group by DimEmployeeId, JobPosition, no, login
but problem is dense_rank doesn't work as I need ;) I want to get something like this:
login | ValidFrom | ValidTo | JobPosition | no |
---|---|---|---|---|
bcde | 2019-07-01 | 2019-09-30 | Project Manager | 1 |
bcde | 2020-01-09 | 2020-06-16 | Head of Center of Excellence | 2 |
bcde | 2020-06-17 | 2020-07-31 | Team Leader | 3 |
bcde | 2020-08-01 | 2021-03-31 | Team Leader | 3 |
bcde | 2021-04-01 | 2021-06-30 | Team Leader | 3 |
bcde | 2021-07-01 | 2021-07-31 | Team Leader | 3 |
bcde | 2021-08-01 | 2021-12-31 | Head of Center of Excellence | 4 |
bcde | 2022-01-01 | 2022-05-09 | Head of Center of Excellence | 4 |
bcde | 2022-02-01 | 2022-05-09 | Head of Center of Excellence | 4 |
bcde | 2022-05-09 | 2099-12-31 | Head of Center of Excellence | 4 |
and after that use query to get final result:
select DimEmployeeId, JobPosition, login, min(ValidFrom), max(ValidTo)
from employeeContracts
group by DimEmployeeId, JobPosition, no, login
Example Data Scheme
CREATE TABLE employeeContracts (
login text,
ValidFrom datetime,
ValidTo datetime,
JobPosition text
);
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2019-07-01', '2019-09-30', 'Project Manager');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2020-01-09', '2020-06-16', 'Head of Center of Excellence');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2020-06-17', '2020-07-31', 'Team Leader');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2020-08-01', '2021-03-31', ' Team Leader');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2021-04-01', '2021-06-30', ' Team Leader');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2021-07-01', '2021-07-31', ' Team Leader');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2021-08-01', '2021-12-31', ' Head of Center of Excellence');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2022-01-01', ' 2022-05-09', 'Head of Center of Excellence');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2022-02-01', '2022-05-09', 'Head of Center of Excellence');
INSERT INTO employeeContracts (login, ValidFrom, ValidTo, JobPosition)
VALUES ('bcde', '2022-05-09', '2099-12-31', 'Head of Center of Excellence');
This can be tested Here