写一个oracle sql查询来获得三个或更多个雇员数量〉100的连续id。例如,在下面的数据中,我们需要id为-5,6,7,8的行。ID为5、6、7、8的行是连续的,并且具有〉100个total_employees。但是,即使total_employees〉100,也不会选择id 10或12,因为它们不是连续的。
输入数据
create table employee(id integer, enroll_date date, total_employees integer);
insert into employee values (1,to_date('01-04-2023','DD-MM-YYYY'),10);
insert into employee values (2,to_date('02-04-2023','DD-MM-YYYY'),109);
insert into employee values (3,to_date('03-04-2023','DD-MM-YYYY'),150);
insert into employee values (4,to_date('04-04-2023','DD-MM-YYYY'),99);
insert into employee values (5,to_date('05-04-2023','DD-MM-YYYY'),145);
insert into employee values (6,to_date('06-04-2023','DD-MM-YYYY'),1455);
insert into employee values (7,to_date('07-04-2023','DD-MM-YYYY'),199);
insert into employee values (8,to_date('08-04-2023','DD-MM-YYYY'),188);
insert into employee values (10,to_date('10-04-2023','DD-MM-YYYY'),188);
insert into employee values (12,to_date('12-04-2023','DD-MM-YYYY'),10);
insert into employee values (13,to_date('13-04-2023','DD-MM-YYYY'),200);
我试图在下面尝试获得连续的组和员工计数标志,但没有弄清楚以获得预期的结果。
select id, enroll_date,total_employees,
case when total_employees>100 then 1 else 0 end emp_flag,
SUM(case when total_employees>100 then 1 else 0 end) OVER (ORDER BY id) AS grp,
id - row_number() over(order by id) as diff, -- group consecutive id's
ROW_NUMBER() OVER (PARTITION BY CASE WHEN total_employees > 100
THEN 1 ELSE 0 END ORDER BY enroll_date) as sal_rn,
id - ROW_NUMBER() OVER (PARTITION BY CASE WHEN total_employees > 100
THEN 1 ELSE 0 END ORDER BY enroll_date) AS sal_grp
from employee
;
3条答案
按热度按时间dzjeubhm1#
这里有一个选择读取代码中的注解。
tuwxkamq2#
在Oracle 12中,可以使用
MATCH_RECOGNIZE
执行逐行模式匹配:或者,首先对
total_employees
进行滤波(而不是在模式匹配期间):对于样本数据,两者都输出:
| 注册日期|ID|员工总数|
| --------------|--------------|--------------|
| 2019 -04- 15 00:00:00|五|一百四十五|
| 2023年4月6日00时00分|六|一四五五|
| 2019 -04-07 00:00:00|七|一百九十九|
| 2019 -04-08 00:00:00|八|一百八十八|
fiddle
ss2ws0br3#
可以使用窗口函数来解决这个间隙和孤岛问题。下面是一个使用
lag()
和窗口count()
的方法:逻辑是将每个id与“前一个”id的值进行比较,并计算它改变了多少次。
结果:
Demo here