一个月内跨部门员工Top N最早打卡
Top N earliest punched in Employees across departments in a month
找两个一个月跨部门最早打卡的员工
数据集,
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
注意:如果部门中的同一个员工在大部分时间打卡时间较早(例如此数据集中人力资源部的 Kumar),则选择他的日期中最早的日期和他旁边打卡的员工。
你可以试试这个 -
SELECT *
FROM (SELECT *
,DENSE_RANK() OVER ( PARTITION BY EmpName, EmpDept ORDER BY EmpJoiningDate) RANK
FROM EMPLOYEE) EMP
WHERE RANK <= 2
我从 DB 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
找两个一个月跨部门最早打卡的员工 数据集,
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
注意:如果部门中的同一个员工在大部分时间打卡时间较早(例如此数据集中人力资源部的 Kumar),则选择他的日期中最早的日期和他旁边打卡的员工。
你可以试试这个 -
SELECT *
FROM (SELECT *
,DENSE_RANK() OVER ( PARTITION BY EmpName, EmpDept ORDER BY EmpJoiningDate) RANK
FROM EMPLOYEE) EMP
WHERE RANK <= 2
我从 DB 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