简体   繁体   English

来自两个不同表的生效日期,匹配特定日期的记录

[英]Effective Dates from Two Different Table, Matching Records as of a Certain Date

I am trying to pull employee records since 1/1/19. 自19年1月1日以来,我正尝试提取员工记录。 I am joining an employee history table to a job code detail table. 我将员工历史记录表添加到工作代码明细表中。 For each employee record, I want to match the job code detail as of the effective date of the employee record. 对于每个员工记录,我要匹配员工记录生效日期的工作代码详细信息。 So if there was an employee record on 2/1/19, I would want the job code detail that was effective at the time; 因此,如果19年2月1日有员工记录,我希望当时有效的职务代码详细信息; and for it not to pull in the top of stack job code detail which may have changed since 2/1/19. 并且不要拖入自2/1/19起可能已更改的堆栈作业代码详细信息的顶部。 I am having issues with the proper coding. 我在使用正确的编码时遇到问题。

I have tried the code below, but not sure it is correct. 我已经尝试了下面的代码,但不确定是否正确。 I joined the J2 table for another report I made to pull the top of stack job code record (ie WHERE J2.EFFDT = NULL). 我加入了J2表,以获取另一个报告以拉出堆栈作业代码记录的顶部(即WHERE J2.EFFDT = NULL)。 Not sure if something similar needs to be used for my current request, and how that is to be incorporated into the WHERE statement. 不知道我的当前请求是否需要使用类似的内容,以及如何将其合并到WHERE语句中。

SELECT
    E.EMPLID AS [EE ID]
    ,E.Name AS [Name]
    ,E.EFFDT AS [Eff Date]
    ,E.COMPANY AS [Co.]
    ,E.JOBCODE AS [Job Code]
    ,E.JOBTITLE [Title]
    ,J.GRADE AS [MRR]
    ,J.BONUS AS [Bonus]
    ,J.OCC_TYPE AS [OCC]
    ,E.EMPL_STATUS_DESC AS [Status]
FROM Employee History AS [E]
LEFT JOIN JobCodeTable AS [J]
    ON E.JOBCODE = J.JOBCODE
LEFT JOIN JobCodeTable AS [J2]
    ON (
        J.JOBCODE = J2.JOBCODE 
        AND 
        J.EFFDT < J2.EFFDT
        )
WHERE E.EMPL_STATUS_DESC = 'Active'
    AND
    E.EFFDT >= '2019-01-01'
    AND
    E.EFFDT >= J.EFFDT

Sample results would be below. 示例结果如下。 This is pulling all the employee records for Jane Doe and John Smith since 1/1/19. 自19年1月1日以来,这刷新了Jane Doe和John Smith的所有员工记录。 In this example each of their job codes had updates done in the job code detail table. 在此示例中,他们的每个工作代码在工作代码详细信息表中都有更新。 Jane's MRR and Bonus attached to her job code were updated in the job code detail table effective 4/1/19. Jane的工作代码附带的MRR和奖金已在工作代码明细表中更新,有效日期为19/4/19。 John's MRR and Bonus attached to his job code were updated in the job code detail table effective 2/10/19. John的MRR和附加在其工作代码上的奖金已在工作代码明细表中更新,日期为19/10/2。 When pulling the employee record, the results should pull the MRR and Bonus that were effective as of the listed date. 在提取员工记录时,结果应提取在所列日期生效的MRR和奖金。

Employee History Table 员工历史记录表

EE ID   || Name       || Eff Date   || Co.   || Job Code || Title || Status    
12345   || Jane Doe   || 5/12/2019  || Apple || A9999    || VP    || Active    
12345   || Jane Doe   || 2/1/2019   || Apple || A9999    || VP    || Active    
54321   || John Smith || 6/5/2019   || Apple || A0002    || Mgr   || Active    
54321   || John Smith || 4/29/2019  || Apple || A0002    || Mgr   || Active    
54321   || John Smith || 1/12/2019  || Apple || A0002    || Mgr   || Active

Job Code Detail Table 职务代码明细表

Job Code|| Title || Eff Date    || MRR || Bonus || OCC 
A9999   || VP    || 4/1/2019    || 5   || 25%   || E     
A9999   || VP    || 1/12/2019   || 4   || 20%   || E    
A0002   || Mgr   || 2/10/2019   || 3   || 15%   || E       
A0002   || Mgr   || 11/01/2018  || 2   || 10%   || E    

What the report should pull 报告应该拉什么

EE ID   || Name       || Eff Date   || Co.   || Job Code || Title || MRR|| Bonus || OCC || Status    
12345   || Jane Doe   || 5/12/2019  || Apple || A9999    || VP    || 5  || 25%   || E   || Active    
12345   || Jane Doe   || 2/1/2019   || Apple || A9999    || VP    || 4  || 20%   || E   || Active    
54321   || John Smith || 6/5/2019   || Apple || A0002    || Mgr   || 3  || 15%   || E   || Active    
54321   || John Smith || 4/29/2019  || Apple || A0002    || Mgr   || 3  || 15%   || E   || Active    
54321   || John Smith || 1/12/2019  || Apple || A0002    || Mgr   || 2  || 10%   || E   || Active

There may be more efficient ways, but I was able to figure it out using SELECT TOP within the SELECT statement. 也许有更有效的方法,但是我能够使用SELECT语句中的SELECT TOP弄清楚。

SELECT E.EMPLID AS [EE ID]
    ,E.Name AS [Name]
    ,E.EFFDT AS [Eff Date]
    ,E.COMPANY AS [Co.]
    ,E.JOBCODE AS [Job Code]
    ,E.JOBTITLE [Title]
    ,(SELECT TOP 1 J.GRADE
        FROM JobCodeTable AS J
        WHERE E.JOBCODE = J.JOBCODE
        AND
        E.EFFDT >= J.EFFDT
        ORDER BY J.EFFDT DESC
    ) AS [MRR]
    ,(SELECT TOP 1 J.BONUS
        FROM JobCodeTable AS J
        WHERE E.JOBCODE = J.JOBCODE
        AND
        E.EFFDT >= J.EFFDT
        ORDER BY J.EFFDT DESC
    ) AS [Bonus]
    ,(SELECT TOP 1 J.OCC_TYPE
        FROM JobCodeTable AS J
        WHERE E.JOBCODE = J.JOBCODE
        AND
        E.EFFDT >= J.EFFDT
        ORDER BY J.EFFDT DESC
    ) AS [OCC]
    ,E.EMPL_STATUS_DESC AS [Status]
FROM Employee History AS [E]
WHERE E.EMPL_STATUS_DESC = 'Active'
    AND
    E.EFFDT >= '2019-01-01'

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

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