oracle Sql查询,以查找代码标记到员工时的最晚开始日期

0yycz8jy  于 2023-03-29  发布在  Oracle
关注(0)|答案(2)|浏览(137)

我有一个分配表,其中包含工会代码详细信息,如下所示-
助理秘书长

NUMBER      START_DATE              END_DATE                POSITION            UNION CODE        MANAGER
10          01-JAN-2006             17-AUG-2017             MANAGER             6790             Cyndy
10          18-AUG-2017             10-JUL-2018             SUPER MANAGER       6790             Cyndy
10          11-JUL-2018             31-12-4712              SUPER MANAGER       6791             Cyndy

10          01-JAN-2006             07-AUG-2017             associate           6790             Bali
10          08-AUG-2017             10-JUL-2019             sr. associate       6790             Bali
10          11-JUL-2019             31-12-4712              MANAGER             6790             Bali

我调整了下面的查询,这样我就可以找到最新工会代码标记到员工的第一个日期。因此输出应该如下所示-

NUMBER      START_DATE              POSITION            UNION CODE        MANAGER
10          11-JUL-2018             SUPER MANAGER       6791             Cyndy
10          01-JAN-2006             associate           6790             Bali

我正在使用下面的查询来查找最新的一行,但我需要工会代码被标记的第一个日期-

select NUMBER, START_dATE,POSITION,UNION_CODE,MANAGER from 
asg
where sysdate between start_Date and end_date
kgsdhlau

kgsdhlau1#

要获得每个number的最新union_code和最早start_date,可以使用ROW_NUMBER解析函数:

SELECT *
FROM   (
  SELECT a.*,
         ROW_NUMBER() OVER (
           PARTITION BY "NUMBER" ORDER BY union_code DESC, start_date ASC
         ) AS rn
  FROM   asg a
)
WHERE  rn = 1;

其中,对于示例数据:

CREATE TABLE ASG ("NUMBER", START_DATE, END_DATE, POSITION, UNION_CODE, MANAGER) AS
SELECT 10, DATE '2006-01-01', DATE '2017-08-17', 'MANAGER',       6790, 'Cyndy' FROM DUAL UNION ALL
SELECT 10, DATE '2017-08-18', DATE '2018-07-10', 'SUPER MANAGER', 6790, 'Cyndy' FROM DUAL UNION ALL
SELECT 10, DATE '2018-07-11', DATE '4712-12-31', 'SUPER MANAGER', 6791, 'Cyndy' FROM DUAL UNION ALL
SELECT 11, DATE '2006-01-01', DATE '2017-08-07', 'associate',     6790, 'Bali'  FROM DUAL UNION ALL
SELECT 11, DATE '2017-08-08', DATE '2019-07-10', 'sr. associate', 6790, 'Bali'  FROM DUAL UNION ALL
SELECT 11, DATE '2019-07-11', DATE '4712-12-31', 'MANAGER',       6790, 'Bali'  FROM DUAL;
  • (假设您有2名员工,他们应该有不同的NUMBER s)*

输出:
| 编号|开始日期|结束日期|职位|联合代码|经理|注册护士|
| --------------|--------------|--------------|--------------|--------------|--------------|--------------|
| 10个|2018年7月11日上午00时00分|2019 - 01 -12 00:00:00|超级经理|小行星6791|辛迪|1|
| 十一|2006年1月1日上午10时|2017-08-07 00:00:00|联营公司|六七九○|八里|1|
fiddle

nwsw7zdq

nwsw7zdq2#

假设NUMBER是员工ID:

SELECT *
FROM ASG A1
WHERE UNION_CODE = (SELECT MAX(UNION_CODE) FROM ASG A2 WHERE A1.NUMBER = A2.NUMBER)
AND START_DATE = (SELECT MIN(START_DATE) FROM ASG A3 WHERE A1.NUMBER = A3.NUMBER)

相关问题