简体   繁体   中英

Query to get employees data in a given date/period

I need help for getting employees work data from a, for me, complex database.

I'll try to resume the database organization:

  1. employees table that, at very minimum is as follows
id    | name
16063 | Jane
17594 | Joe
  1. history table with dates of start/end working periods:
id   | id_employee | start    | end
1518 | 16063       | 20110701 | 20991231
1576 | 17594       | 20201123 | 20210715
1577 | 17594       | 20210716 | 20991231
  1. employment_type
id | description
3  | permanent
4  | fixed-term
  1. employment_status
id   | id_employee | start    | id_employment
1518 | 16063       | 20210901 | 3
1575 | 17594       | 20201123 | 4
1576 | 17594       | 20210716 | 3
  1. positions
id | description
5  | Assistant
26 | Collaborator
56 | Manager
  1. position_status
id   | id_employee | start    | id_position
1545 | 16063       | 20190101 | 5
1546 | 16063       | 20210901 | 26
1603 | 17594       | 20201123 | 56
  1. levels
id | description
7  | C
9  | D
20 | ME1B
  1. level_status
id   | id_employee | start    | id_level
1525 | 16063       | 20190101 | 7
1526 | 16063       | 20210901 | 9
1583 | 17594       | 20201123 | 20

I need help for a query that, fi, in the period January 1st and December 31th of 2021 returns:

id    | name | employment | position     | level | start      | end
16063 | Jane | permanent  | assistant    | C     | 20110701   | 20991231
16063 | Jane | permanent  | collaborator | D     | 20110701   | 20991231
17594 | Joe  | fixed-term | manager      | ME1B  | 20201123   | 20210715
17594 | Joe  | permanent  | manager      | ME1B  | 20210716   | 20991231

while testing only January 1st 2021, it should return:

id    | name | employment | position     | level | start    | end
16063 | Jane | permanent  | assistant    | C     | 20110701 | 20991231
17594 | Joe  | fixed-term | manager      | ME1B  | 20201123 | 20210715

I'm using the following query:

SELECT employees.id,employees.name,employment_type.description,positions.description,levels.description,history.start,history.end
FROM history 
LEFT JOIN employees ON employees.id=history.id_employee 
LEFT JOIN employment_status ON employees.id=employment_status.id_employee 
LEFT JOIN employment_type ON employment_status.id_employment=employment_type.id 
LEFT JOIN position_status ON employees.id=position_status.id_employee 
LEFT JOIN positions ON position_status.id_position=positions.id 
LEFT JOIN level_status ON employees.id=level_status.id_employee 
LEFT JOIN levels ON level_status.id_level=levels.id 
WHERE (history.start <= '20210101' OR history.end <= '20211231');

this gives me an incorrect output:

16063 | Jane | permanent  | assistant    | C    | 20110701 | 20991231
16063 | Jane | permanent  | assistant    | D    | 20110701 | 20991231
16063 | Jane | permanent  | collaborator | C    | 20110701 | 20991231
16063 | Jane | permanent  | collaborator | D    | 20110701 | 20991231
17594 | Joe  | permanent  | manager      | ME1B | 20201123 | 20210715
17594 | Joe  | fixed-term | manager      | ME1B | 20201123 | 20210715

As you can see, for Jane it created all combinations of positions and levels while for Joe for "permanent" it reported same dates of fixed-term one.

This is the sql of the sample database (if of any help) Thanks in advance

BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "employees" (
    "id"    INTEGER NOT NULL,
    "name"  TEXT NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "employment_type" (
    "id"    INTEGER NOT NULL,
    "description"   TEXT NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "positions" (
    "id"    INTEGER NOT NULL,
    "description"   TEXT NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "levels" (
    "id"    INTEGER NOT NULL,
    "description"   TEXT NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "employment_status" (
    "id"    INTEGER NOT NULL,
    "id_employee"   INTEGER NOT NULL,
    "start" INTEGER NOT NULL,
    "id_employment" INTEGER NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "history" (
    "id"    INTEGER NOT NULL,
    "id_employee"   INTEGER NOT NULL,
    "start" INTEGER NOT NULL,
    "end"   INTEGER NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "level_status" (
    "id"    INTEGER NOT NULL,
    "id_employee"   INTEGER NOT NULL,
    "start" INTEGER NOT NULL,
    "id_level"  INTEGER NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "position_status" (
    "id"    INTEGER NOT NULL,
    "id_employee"   INTEGER NOT NULL,
    "start" INTEGER NOT NULL,
    "id_position"   INTEGER NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);
INSERT INTO "employees" VALUES (16063,'Jane');
INSERT INTO "employees" VALUES (17594,'Joe');
INSERT INTO "employment_type" VALUES (3,'permanent');
INSERT INTO "employment_type" VALUES (4,'fixed-term');
INSERT INTO "positions" VALUES (5,'Assistant');
INSERT INTO "positions" VALUES (26,'Collaborator');
INSERT INTO "positions" VALUES (56,'Manager');
INSERT INTO "levels" VALUES (7,'C');
INSERT INTO "levels" VALUES (9,'D');
INSERT INTO "levels" VALUES (20,'ME1B');
INSERT INTO "employment_status" VALUES (1,16063,20210901,3);
INSERT INTO "employment_status" VALUES (2,17594,20201123,4);
INSERT INTO "employment_status" VALUES (3,17594,20210716,3);
INSERT INTO "history" VALUES (1,16063,20110701,20991231);
INSERT INTO "history" VALUES (2,17594,20201123,20210715);
INSERT INTO "history" VALUES (3,17594,20210716,20991231);
INSERT INTO "level_status" VALUES (1,16063,20190101,7);
INSERT INTO "level_status" VALUES (2,16063,20210901,9);
INSERT INTO "level_status" VALUES (3,17594,20201123,20);
INSERT INTO "position_status" VALUES (1,16063,20190101,5);
INSERT INTO "position_status" VALUES (2,16063,20210901,26);
INSERT INTO "position_status" VALUES (3,17594,20201123,56);
COMMIT;

Not sure if this is the most efficient way to do it.

But with enough row_numbers you get rid of the duplications.

Also note that this will return the latest from employee_type, position & level, regardless of history.
Since only history has a criteria for the dates.

 SELECT emp.id as emp_id, emp.name as emp_name, emptype.description as type_desc, pos.description as pos_desc, lvl.description as lvl_desc, hist.start as hist_start, hist.end as hist_end FROM employees AS emp INNER JOIN ( select id_employee, h.start, h.end, row_number() over (partition by id_employee order by start desc) as rn from history h where (h.start <= '20210101' OR h.end <= '20211231') ) AS hist ON hist.id_employee = emp.id AND hist.rn = 1 LEFT JOIN ( select id_employee, id_employment, row_number() over (partition by id_employee order by start desc) as rn from employment_status ) AS empstat ON empstat.id_employee = emp.id AND empstat.rn = 1 LEFT JOIN employment_type AS emptype ON emptype.id = empstat.id_employment LEFT JOIN ( select id_employee, id_position, row_number() over (partition by id_employee order by start desc) as rn from position_status ) AS posstat ON posstat.id_employee = emp.id AND posstat.rn = 1 LEFT JOIN positions AS pos ON pos.id = posstat.id_position LEFT JOIN ( select id_employee, id_level, row_number() over (partition by id_employee order by start desc) as rn from level_status ) AS lvlstat ON lvlstat.id_employee = emp.id AND lvlstat.rn = 1 LEFT JOIN levels AS lvl ON lvl.id = lvlstat.id_level;
emp_id emp_name type_desc pos_desc lvl_desc hist_start hist_end
16063 Jane permanent Collaborator D 20110701 20991231
17594 Joe permanent Manager ME1B 20201123 20210715

Demo on db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM