简体   繁体   English

查询以获取给定日期/期间的员工数据

[英]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 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:我需要一个查询的帮助,fi 在 2021 年 1 月 1 日和 12 月 31 日期间返回:

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:虽然仅在 2021 年 1 月 1 日进行测试,但它应该返回:

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:这给了我一个不正确的 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.正如您所看到的,对于 Jane,它创建了所有头寸和级别组合,而对于 Joe,它为“永久”报告了相同的定期日期组合。

This is the sql of the sample database (if of any help) Thanks in advance这是示例数据库的 sql(如果有帮助)提前致谢

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.但是有了足够的 row_numbers,你就可以摆脱重复。

Also note that this will return the latest from employee_type, position & level, regardless of history.另请注意,无论历史记录如何,这都会从employee_type、position 和级别返回最新的。
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_id emp_name emp_name type_desc type_desc pos_desc pos_desc lvl_desc lvl_desc hist_start hist_start hist_end hist_end
16063 16063 Jane permanent永恒的 Collaborator合作者 D D 20110701 20110701 20991231 20991231
17594 17594 Joe permanent永恒的 Manager经理 ME1B ME1B 20201123 20201123 20210715 20210715

Demo on db<>fiddle here关于db<>fiddle 的演示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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