English 中文(简体)
如何在甲骨文中取得最新的员工记录?
原标题:How to get the latest employee record in oracle?
  • 时间:2012-05-28 14:14:17
  •  标签:
  • sql
  • oracle

我有一个员工表,上面有诸如 enva_id、 名字、 姓氏、 地区名称、 身份和有效日期等栏目。

雇员表可列出同一雇员的多个条目,该雇员的有效日期和地位不同。

雇员可拥有两个身份保留者和加入者。

id     emp_id    firstname     region    status     effective_date  
1       1         James        Asia      Joiner     1-Jan-2012 
2       1         James        UK        Leaver     1-Aug-2012
3       1         James        USA       Joiner     1-Aug-2012
4       1         James        Asia      Leaver     1-May-2012
5       1         James        UK        Joiner     1-May-2012
6       1         James        USA       Leaver     1-Sep-2012

根据雇员表中的上述数据,如果我想获得截至2012年1月1日的James的最新记录,我会得到ID=1的记录。

如果我想获得2012年5月1日的果酱最新记录, 我会得到ID=5的记录

如果我想获得2012年8月1日的果酱最新记录 我会拿到ID=3的记录

如果我想获得2012年9月1日的果酱最新记录,

以下查询正确后给我最新记录

SELECT 
        emp_id, 
        MAX(effective_date) AS latest_effective_date
FROM 
        EMPLOYEE
GROUP BY 
        emp_id

但是,我如何得到其他列,如名、区域等。

如果我把它们按条款放入选择条款或组内, 我不仅得到最新的记录, 还有其他的记录。

问题回答
SELECT * FROM 
( SELECT  
    e.*,
    ROW_NUMBER() OVER (partition by emp_id order by effective_date DESC) r
FROM  
    EMPLOYEE  e)
WHERE r = 1;

上面将赏赐你一个最高效力的记录,供你们各行各业使用。

您的第二个要求是返回给定日期的记录, 此查询应完整存档 : @ info: whatsthis

("状态ASC" - 将关注 采取"参军"状态 如果有"leaver"在同一日期。 )

 SELECT * FROM 
( SELECT  
    e.*,
    ROW_NUMBER() OVER (partition by emp_id order by effective_date DESC, status ASC) r
FROM  
    EMPLOYEE  e
WHERE effective_date <=  <your desired date> )
WHERE r=1;

您需要在内部加入您已经回到员工表格的查询, 以限制记录 :

SELECT  Emp.*
FROM    Employee Emp
        INNER JOIN
        (   SELECT  Emp_ID, MAX(effective_date) AS latest_effective_date
            FROM    Employee
            GROUP BY Emp_ID
        ) MaxEmp
            ON Emp.Emp_ID = MaxEmp.Emp_ID
            AND Emp.Effective_Date = MaxEmp.latest_effective_date

尝试 :

SELECT *
FROM EMPLOYEE emp
INNER JOIN (SELECT max(id) AS id
        emp_id, 
        MAX(effective_date) AS latest_effective_date
FROM 
        EMPLOYEE
GROUP BY 
        emp_id) AS employee_1 on emp.id = employee_1.id

您输入的查询不一定像您之前所说的, 以 ids 3, 5, 6 返回记录, 因为在这种情况下:

2       1         James        Asia      Leaver     1-May-2012
3       1         James        UK        Joiner     1-May-2012

两个行的有效日期相等,很可能以 ID 2 而不是 3 返回记录。

尝试在表格中添加时间或在有效日期列中添加时间, 这样您就可以在确定的日期从用户那里获得最新结果 。

试试这个

SELECT  
  MAX(id) KEEP (DENSE_RANK FIRST ORDER BY effective_date DESC) id,
  MAX(emp_id) KEEP (DENSE_RANK FIRST ORDER BY effective_date DESC) emp_id,
  MAX(firstname) KEEP (DENSE_RANK FIRST ORDER BY effective_date DESC) firstname,
  MAX(status) KEEP (DENSE_RANK FIRST ORDER BY effective_date DESC) status,
  MAX(effective_date) KEEP (DENSE_RANK FIRST ORDER BY effective_date DESC) effective_date
FROM Employee GROUP BY firstname

问题:Get 雇员记录加入公司的最新记录

Solution :
Step 1. Get latest date when employees recently joined a company
Step 2. Get records of all employees who joined on that date

select * 
from EMPLOYEE 
where effective_date in (
    SELECT MAX(effective_date) AS latest_effective_date 
    FROM EMPLOYEE GROUP BY emp_id
);




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签