一个月来,跨部门员工进入前N名的时间最早

时间:2018-07-30 12:39:13

标签: oracle oracle11g dense-rank

查找一个月来跨部门的最早打孔员工中的两个 数据集,

Id   EmpName         EmpPunchInTime        EmpDept
1      Dharma        26-JAN-2018 08:45     Engineering
2      Gumpina       26-JAN-2018 08:46     Finance
3      Kumar         26-JAN-2018 08:47     HR
4      Kamal         26-JAN-2018 08:48     Engineering
5      Muruges       26-JAN-2018 08:49     Finance
6      Gumpina       25-JAN-2018 09:01     Finance
7      Kumar         25-JAN-2018 08:42     HR
8      Biju          25-JAN-2018 08:40     Engineering
9      Divya         25-JAN-2018 08:49     HR
10     Vali          24-JAN-2018 08:52     Engineering

The expected output is,
Id   EmpName       EmpPunchInTime        EmpDept
8      Biju          25-JAN-2018 08:40     Engineering
1      Dharma        26-JAN-2018 08:45     Engineering
2      Gumpina       26-JAN-2018 08:46     Finance
5      Muruges       26-JAN-2018 08:49     Finance
7      Kumar         25-JAN-2018 08:42     HR
9      Divya         25-JAN-2018 08:49     HR

注意:如果某个部门中的同一名员工在大多数时间里都在打孔(例如此数据集的HR部门的Kumar),则从他的日期中选择最早的日期,然后在旁边打孔的员工。

2 个答案:

答案 0 :(得分:0)

您可以尝试-

SELECT *
FROM (SELECT *
            ,DENSE_RANK() OVER ( PARTITION BY EmpName, EmpDept ORDER BY EmpJoiningDate) RANK
      FROM EMPLOYEE) EMP
WHERE RANK <= 2

答案 1 :(得分:0)

我从数据库stackexchange那里得到了答案。

with data as
(
  select 1 as id, 'Dharma' as empname, to_date('26-JAN-2018 08:45', 'DD-MON-YYYY HH24:MI') as emppunchintime, 'Engineering' as empdept from dual union all
  select 2 ,      'Gumpina',           to_date('26-JAN-2018 08:46', 'DD-MON-YYYY HH24:MI'),                   'Finance'                from dual union all
  select 3 ,      'Kumar',             to_date('26-JAN-2018 08:47', 'DD-MON-YYYY HH24:MI'),                   'HR'                     from dual union all
  select 4 ,      'Kamal' ,            to_date('26-JAN-2018 08:48', 'DD-MON-YYYY HH24:MI'),                   'Engineering'            from dual union all
  select 5 ,      'Muruges',           to_date('26-JAN-2018 08:49', 'DD-MON-YYYY HH24:MI'),                   'Finance'                from dual union all
  select 6 ,      'Gumpina',           to_date('25-JAN-2018 09:01', 'DD-MON-YYYY HH24:MI'),                   'Finance'                from dual union all
  select 7 ,      'Kumar',             to_date('25-JAN-2018 08:42', 'DD-MON-YYYY HH24:MI'),                   'HR'                     from dual union all
  select 8 ,      'Biju',              to_date('25-JAN-2018 08:40', 'DD-MON-YYYY HH24:MI'),                   'Engineering'            from dual union all
  select 9 ,      'Divya',             to_date('25-JAN-2018 08:49', 'DD-MON-YYYY HH24:MI'),                   'HR'                     from dual union all
  select 10,      'Vali',              to_date('24-JAN-2018 08:52', 'DD-MON-YYYY HH24:MI'),                   'Engineering'            from dual
)
select f.id, f.empname, f.emppunchintime, f.empdept from 
(
  select e.*, row_number() over (partition by e.empdept order by to_char(e.emppunchintime, 'HH24:MI')) as rn2 from
  (
    select d.*, row_number() over (partition by d.empname order by to_char(d.emppunchintime, 'HH24:MI')) as rn from data d
  ) e
  where e.rn = 1
) f
where f.rn2 <= 2
order by f.empdept, f.emppunchintime;

感谢您的时间@ankit