简体   繁体   English

当单个员工 ID 具有多个日期值且仅需要最新日期时,如何使用 max function 返回单个记录

[英]How do I return a single record using max function when a single employee-ID has several date values and Need Only Latest Date

We have Employees that will have several BEG_DATE returns and we only need the record that has the latest date.我们的员工将有多个 BEG_DATE 返回,我们只需要具有最新日期的记录。 I can use the MAX to return that date when I apply the second query - but how can I use it on this query below?当我应用第二个查询时,我可以使用 MAX 返回该日期 - 但是如何在下面的这个查询中使用它?

SELECT REPLACE(EMPLOYEE.EMPLOYEE , ',', '') AS EMPLOYEE_ID, A_VALUE, DB.EMPLOYEE.EMP_STATUS, DB.HRHISTORY.BEG_DATE
    FROM DB.PERSHST
    INNER JOIN WDWASIS ON WDWASIS.FILENAME = 'LEAVE_TYPE_NAME' 
           AND PERSHST.REASON_01 = WDWASIS.ALPHA_DEF2
           AND PERSHST.REASON_02 = WDWASIS.ALPHA_DEF3
    INNER JOIN DB.EMPLOYEE ON DB.PERSHST.EMPLOYEE = DB.EMPLOYEE.EMPLOYEE
    INNER JOIN DB.HRHISTORY ON DB.HRHISTORY.EMPLOYEE = DB.EMPLOYEE.EMPLOYEE AND DB.HRHISTORY.COMPANY = DB.EMPLOYEE.COMPANY AND DB.HRHISTORY.FLD_NBR = 20 
    WHERE DB.EMPLOYEE.EMP_STATUS LIKE 'L%' AND DB.HRHISTORY.A_VALUE LIKE 'L%' 

This query works of course when I have a single employee - but how can I feed this into the query above?当我有一个员工时,这个查询当然可以工作 - 但是我怎样才能将它输入到上面的查询中呢?

SELECT DB.HRHISTORY.EMPLOYEE, MAX(DB.HRHISTORY.BEG_DATE)
FROM DB.HRHISTORY
WHERE DB.HRHISTORY.EMPLOYEE ='0'
GROUP BY DB.HRHISTORY.EMPLOYEE

Create a table with the max(date) per employee and join to it on employeeId and date.创建一个包含每个员工的 max(date) 的表,并在 employeeId 和 date 上加入它。 Or, something like this...或者,像这样的东西......

    with emp_max_date as
        (
        SELECT DB.HRHISTORY.EMPLOYEE as employee_id, MAX(DB.HRHISTORY.BEG_DATE) as recent_date
        FROM DB.HRHISTORY
        GROUP BY DB.HRHISTORY.EMPLOYEE
        )
    SELECT REPLACE(EMPLOYEE.EMPLOYEE , ',', '') AS EMPLOYEE_ID, A_VALUE, DB.EMPLOYEE.EMP_STATUS, DB.HRHISTORY.BEG_DATE
        FROM DB.PERSHST
        INNER JOIN WDWASIS ON WDWASIS.FILENAME = 'LEAVE_TYPE_NAME' 
               AND PERSHST.REASON_01 = WDWASIS.ALPHA_DEF2
               AND PERSHST.REASON_02 = WDWASIS.ALPHA_DEF3
        INNER JOIN DB.EMPLOYEE ON DB.PERSHST.EMPLOYEE = DB.EMPLOYEE.EMPLOYEE
        INNER JOIN DB.HRHISTORY ON DB.HRHISTORY.EMPLOYEE = DB.EMPLOYEE.EMPLOYEE AND DB.HRHISTORY.COMPANY = DB.EMPLOYEE.COMPANY AND DB.HRHISTORY.FLD_NBR = 20 
        join emp_max_date eb on eb.employee_id = db.employee.employee and eb.recent_date = db.hrhistory.beg_date
        WHERE DB.EMPLOYEE.EMP_STATUS LIKE 'L%' AND DB.HRHISTORY.A_VALUE LIKE 'L%'

暂无
暂无

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

相关问题 SQL存储过程为每个ID返回带有最大日期的单个记录 - SQL stored procedure return single record per ID with Max Date for that ID 如何使用 SQL 返回最新日期的案例记录 - How to I return a case record with latest date using SQL MS Access:如何查找最新日期且仅处于“工作”或“恢复”状态的员工? - MS Access: How do I find an employee with the latest date with "Working" or "Reinstated" status only? 选择按最大日期返回最新记录 - Select recond by max date return latest record 表每年每位员工有一条记录。 如何选择该雇员最近一年的记录? - Table has one record per employee per year. How do I select the record for each employee for the latest year for that employee? 如果最大日期不是唯一的,则需要一个SQL查询来获得单个最大日期 - Need a SQL query to get a single, max date when the max date is not unique 如何设置子查询以获取具有最新日期和最大 ID 的单个记录? - How can I set subquery to get single records with latest date and maximum id? SQL - 只需要最大日期的记录 - SQL - Only need the record with Max Date 使用 SQL 和 Matillion for Employee 表,我只需要提取最近的员工雇用日期 - Using SQL and Matillion for Employee table, I need to pull only the most recent employee hire date 如何根据 BigQuery 中的最新日期为每个客户返回一行? - How can I return one single row for each customer based on the latest date in BigQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM